如何将这些 XSLT 文档进行 DRY-ed [英] How can these XSLT documents be DRY-ed

查看:14
本文介绍了如何将这些 XSLT 文档进行 DRY-ed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了两页,现在我想知道我是否可以将它们晾干.

这里是 XSLT:

首页:http://pastebin.com/yuZL913Wdagboek-page:http://pastebin.com/6FGYvpvf(已编辑)

罗洛夫

抱歉,其中一个链接是错误的.我编辑了这个.

我的问题是如何干燥这些以便我可以重复使用零件.只有

不同.我想我理解填空,但它是一个大的 xslt 文件.如果没有命名模板和调用模板,这可能吗

解决方案

这是一种方法——我称之为填空"将内容与处理分开并参数化处理:

渲染文件(c:/temp/delete/nc3.xml):

<gen:data from="params" mode="top"/><身体><div id="外层"><div id="容器"><div id="搜索"><form method="get" id="searchform" action="http://www.notepadchaos.com/"><input type="text" value="" name="s" id="s" class="txtField"/><input type="submit" id="searchsubmit" class="btnSearch" value="Find It"/></表单>

<div id="标题"><h2>塔玛拉·沃本</h2>

<div id="内容"><div class="col01"><div class="post"><h3><gen:data from="entry/title"/><div class="post-inner"><gen:data from="section/entry/tekst"/></div></div></div><div class="side-columns"><div class="col02"><div class="pages"><!-- hier komen de losse pagina-->

<div class="pages-bottom"></div><div class="categories-upper"></div><div class="categories"><!-- hier komt het 菜单-->

<div class="categories-btm"></div>

<div class="col03"><div class="recent-post" ><!-- hier komen de recente 帖子-->

<div class="postit-bottom"></div>

<br style="clear:both"/>

<gen:data from="params" mode="down"/>

数据文件 (c:/temp/delete/data.xml):

<参数><今天>2011-12-29</今天><当前时间>22:17</当前时间><今年>2011</今年><本月>12</本月><今天>29</今天><timezone>+01:00</timezone><website-name>Tamara Wobben</website-name><page-title>首页</page-title><root>http://test.tamarawobben.nl</root><workspace>http://test.tamarawobben.nl/workspace</workspace><root-page>首页</root-page><当前页面>首页</当前页面><current-page-id>6</current-page-id><current-path>/?debug=xml</current-path><父路径>/</父路径><current-url>http://test.tamarawobben.nl/?debug=xml</current-url><上传限制>2097152</上传限制><symphony-version>2.2.5</symphony-version><cookie-username>xxxx</cookie-username><cookie-pass>xxxxx</cookie-pass><site-mode>live</site-mode></参数><事件/><图像><section id="7" handle="images">图片</section><entry id="13"><image size="22 KB" path="/images" type="image/jpeg"><文件名>img_5874.jpg<meta creation="2011-12-19T18:40:04+01:00" width="400" height="267"/></图像></条目></图像><最近的帖子><section id="9" handle="dagboek">Dagboek</section><entry id="15"><datum time="00:00" weekday="2">2005-02-22</datum><title handle="7-weken-echo">7 weken echo</titel></条目></最近的帖子><部分><section id="6" handle="sections">Sections</section><entry id="12"><title handle="even-voorstellen">Even Voorstellen</title><tekst><p>浮点数:img_5874.jpg</p>55 56 p Naam:塔玛拉·沃本 (Tamara Wobben)57Geboorte gewicht:2000 克<br/>58Geboorte lente:44 厘米.<br/>59Geboortedatum:2005 年 9 月 1 日</tekst></条目></节></数据>

XSLT 代码:

<xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:strip-space elements="*"/><xsl:param name="pFormPath" select="'file:///c:/temp/delete/nc3.xml'"/><xsl:param name="pDataPath" select="'file:///c:/temp/delete/data.xml'"/><xsl:variable name="vFormDoc" select="document($pFormPath)"/><xsl:variable name="vDataDoc" select="document($pDataPath)"/><xsl:template match="node()|@*"><xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:模板><xsl:template match="/"><xsl:apply-templates select="$vFormDoc/*"/></xsl:模板><xsl:模板匹配=基因:数据[@from='参数'和@mode='top']"><!-- 使用 $vDataDoc 来填充这个 -->上一页</xsl:模板><xsl:模板匹配=gen:data[@from='entry/Title']"><!-- 使用 $vDataDoc 来填充这个 -->登录</xsl:模板><xsl:模板匹配=gen:data[@from='section/entry/tekst']"><!-- 使用 $vDataDoc 来填充这个 -->S E C T I O N E N T R Y T E X T</xsl:模板><xsl:模板匹配=基因:数据[@from='参数'和@mode='向下']"><!-- 使用 $vDataDoc 来填充这个 -->D O W N S T U F</xsl:模板></xsl:stylesheet>

源 XML 文件(未使用):

对任何 XML 文件(未使用)执行上述转换时,渲染文件将填充数据文件中的数据.处理占位符 gen:data 元素的模板是虚拟的,但仍然展示了这种处理是如何工作的.

处理结果:

上一页<身体><div id="外层"><div id="容器"><div id="搜索"><form method="get" id="searchform" action="http://www.notepadchaos.com/"><input type="text" value="" name="s" id="s" class="txtField"><input type="submit" id="searchsubmit" class="btnSearch" value="Find It"></form>

<div id="标题"><h2>塔玛拉·沃本</h2>

<div id="内容"><div class="col01"><div class="post"><h3><gen:data from="entry/title"></gen:data><div class="post-inner">S E C T I O N E N T R Y T E X T

<div class="side-columns"><div class="col02"><div class="pages"><!-- hier komen de losse pagina-->

<div class="pages-bottom"></div><div class="categories-upper"></div><div class="categories"><!-- hier komt het 菜单-->

<div class="categories-btm"></div>

<div class="col03"><div class="最近的帖子"><!-- hier komen de recente 帖子-->

<div class="postit-bottom"></div>

</div><br style="clear:both"></div>D O W N S T U F

请注意:

  1. 此代码可以使用来自任何数据文档的数据(同样作为外部参数传递的路径)填充任何渲染文档(作为外部参数传递的路径).因此,可以创建填充有不同数据的不同输出/格式.

  2. 要被实时内容"替换的占位符(gen:data 元素)可以有不同的格式和语义——没有任何想象力的限制.

  3. 编辑器(非 XSLT 专家)可以独立于彼此并独立于 XSLT 开发人员处理一个或多个呈现文档.

  4. 实现了更高程度的可重用性、灵活性和可维护性.

I have made two pages and now I wonder if I can DRY them.

Here the XSLT:

Frontpage: http://pastebin.com/yuZL913W dagboek-page: http://pastebin.com/6FGYvpvf (Edited)

Roelof

Sorry one of the links is wrong. I edited this.

My question is how to DRY these so I can reuse parts. Only the <div id="posts"> is different. I think I understand the fill in the blanks but it is one big xslt file. Is this possible without named templates and call-templates

解决方案

Here is one way -- what I call "Fill-in-the-blanks" to separate content from processing and to parameterize processing:

Rendering file (c:/temp/delete/nc3.xml):

<html  xmlns:gen="gen:gen">
<gen:data from="params" mode="top"/>
<body >
             <div id="outer">
                 <div id="container">
                     <div id="search">
                         <form method="get" id="searchform" action="http://www.notepadchaos.com/">
                             <input type="text" value="" name="s" id="s" class="txtField" />
                             <input type="submit" id="searchsubmit" class="btnSearch" value="Find It " />
                        </form>
                     </div>
                         <div id="title">
                             <h2>Tamara Wobben</h2>
                        </div>
                     </div>
                     <div id="content">
                        <div class="col01">
                                <div class="post">
                                     <h3> <gen:data from="entry/title"/> </h3>
                                    <div class="post-inner">
                             <gen:data from="section/entry/tekst" />
                               </div></div></div>
                              <div class="side-columns">
                             <div class="col02">
                                 <div class="pages">
                                     <!-- hier komen de losse pagina's -->
                                 </div>
                                 <div class="pages-bottom"></div>
                                 <div class="categories-upper"></div>
                                 <div class="categories">
                                    <!-- hier komt het menu  -->
                                </div>
                                 <div class="categories-btm"></div>
                                </div>
                             <div class ="col03">
                                <div class="recent-post" >
                                <!-- hier komen de recente posts -->
                                </div>
                                <div class="postit-bottom"></div>
                            </div>
                         </div>
<br style="clear:both" />
                     </div>
                     <gen:data from="params" mode="down"/>
                </div>
            </body>
</html>

Data file (c:/temp/delete/data.xml):

<data>
    <params>
    <today>2011-12-29</today>
    <current-time>22:17</current-time>
    <this-year>2011</this-year>
    <this-month>12</this-month>
    <this-day>29</this-day>
    <timezone>+01:00</timezone>
    <website-name>Tamara Wobben</website-name>
    <page-title>frontpage</page-title>
    <root>http://test.tamarawobben.nl</root>
    <workspace>http://test.tamarawobben.nl/workspace</workspace>
    <root-page>frontpage</root-page>
    <current-page>frontpage</current-page>
    <current-page-id>6</current-page-id>
    <current-path>/?debug=xml</current-path>
    <parent-path>/</parent-path>
    <current-url>http://test.tamarawobben.nl/?debug=xml</current-url>
    <upload-limit>2097152</upload-limit>
    <symphony-version>2.2.5</symphony-version>
    <cookie-username>xxxx</cookie-username>
    <cookie-pass>xxxxx</cookie-pass>
    <site-mode>live</site-mode>
</params>
    <events />
          <image>
              <section id="7" handle="images">Images</section>
              <entry id="13">
                 <image size="22 KB" path="/images" type="image/jpeg">
                    <filename>img_5874.jpg</filename>
                    <meta creation="2011-12-19T18:40:04+01:00" width="400" height="267" />
                 </image>
             </entry>
         </image>
         <recent-posts>
              <section id="9" handle="dagboek">Dagboek</section>
                   <entry id="15">
                         <datum time="00:00" weekday="2">2005-02-22</datum>
                         <titel handle="7-weken-echo">7 weken echo</titel>
                   </entry>
         </recent-posts>
<section>
      <section id="6" handle="sections">Sections</section>
          <entry id="12">
               <title handle="even-voorstellen">Even Voorstellen</title>
               <tekst><p>FLOAT : img_5874.jpg</p> 55 56<p>Naam : Tamara Wobben<br /> 57Geboorte gewicht : 2000 gram<br /> 58Geboorte lengte : 44 cm.<br /> 59Geboortedatum : 1 september 2005  </p>
                 </tekst>
            </entry>
    </section>
</data>

XSLT code:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:gen="gen:gen" exclude-result-prefixes="gen">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pFormPath" select=
     "'file:///c:/temp/delete/nc3.xml'"/>
 <xsl:param name="pDataPath" select=
     "'file:///c:/temp/delete/data.xml'"/>

 <xsl:variable name="vFormDoc" select="document($pFormPath)"/>
 <xsl:variable name="vDataDoc" select="document($pDataPath)"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <xsl:apply-templates select="$vFormDoc/*"/>
 </xsl:template>

 <xsl:template match=
 "gen:data
    [@from='params'
    and
     @mode='top'
    ]">

  <!-- Use $vDataDoc to populate this -->

  T O P   S T U F
 </xsl:template>

 <xsl:template match=
 "gen:data[@from='entry/Title']">

  <!-- Use $vDataDoc to populate this -->

  E N T R Y   T I T L E
 </xsl:template>
 <xsl:template match=
 "gen:data[@from='section/entry/tekst']">

  <!-- Use $vDataDoc to populate this -->

   S E C T I O N   E N T R Y   T E X T
 </xsl:template>

 <xsl:template match=
 "gen:data
    [@from='params'
    and
     @mode='down'
    ]">

  <!-- Use $vDataDoc to populate this -->

  D O W N   S T U F
 </xsl:template>

</xsl:stylesheet>

Source XML file (not used):

<t/>

When the above transformation is performed on any XML file (not used), the rendering file is populated with data from the data file. The templates that process the placeholder gen:data elements, are dummy but still demonstrate how this processing works.

The result of the processing:

<html xmlns:gen="gen:gen">



   T O P   S T U F

   <body>
      <div id="outer">
         <div id="container">
            <div id="search">
               <form method="get" id="searchform" action="http://www.notepadchaos.com/"><input type="text" value="" name="s" id="s" class="txtField"><input type="submit" id="searchsubmit" class="btnSearch" value="Find It "></form>
            </div>
            <div id="title">
               <h2>Tamara Wobben</h2>
            </div>
         </div>
         <div id="content">
            <div class="col01">
               <div class="post">
                  <h3>
                     <gen:data from="entry/title"></gen:data>
                  </h3>
                  <div class="post-inner">



                     S E C T I O N   E N T R Y   T E X T

                  </div>
               </div>
            </div>
            <div class="side-columns">
               <div class="col02">
                  <div class="pages">
                     <!-- hier komen de losse pagina's -->
                  </div>
                  <div class="pages-bottom"></div>
                  <div class="categories-upper"></div>
                  <div class="categories">
                     <!-- hier komt het menu  -->
                  </div>
                  <div class="categories-btm"></div>
               </div>
               <div class="col03">
                  <div class="recent-post">
                     <!-- hier komen de recente posts -->
                  </div>
                  <div class="postit-bottom"></div>
               </div>
            </div><br style="clear:both"></div>



         D O W N   S T U F

      </div>
   </body>
</html>

Do note:

  1. This code can populate any rendering document (path passed as an external parameter) using the data from any data document (again path passed as an external parameter). Thus it becomes possible to create different outputs/formats populated with different data.

  2. The placeholders (gen:data elements) to be replaced with "live content" can have different format and semantics -- no limits to one's imagination.

  3. Editors (non-XSLT experts) can work on one or more rendering documents independently from each other and from the XSLT developers.

  4. A higher degree of reusability, flexibility and maintainability is achieved.

这篇关于如何将这些 XSLT 文档进行 DRY-ed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆