我想了解的是两次传递或多阶段处理的方法,并且能够在下面的场景中应用它。
我仍然在我的XSLT培训轮上--这里的大部分内容都是在同事或在线资源的帮助下组装的,并对其进行了修改以适应需要。如果不清楚或者我的理解有缺陷,请原谅(任何改进工作部件的建议也欢迎)
下面的xml是我输入的XML的修改样本。下面显示了一个条目和感兴趣的节点(加上上下文的一点模糊)。
<root><nNumber id="N472131">
<symbols>
<symbol href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/IP68_def.ai"/>
<symbol href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/Bin_2_def.ai"/>
<symbol href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/CE0197_def.ai"/>
<symbol href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/Recycle_def.ai"/>
<symbol href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/Humidity0-90_def.ai"/>
<symbol href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/Temp-10-55_def.ai"/>
</symbols>
</nNumber></root>我的XSLT通过@id上提供的参数进行筛选,并对@id -> <id></id>进行小范围的重新排列。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:param name="search"></xsl:param>
<!-- Identity transform - copies all elements and attributes. -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Filter nNumber nodes by ID. -->
<xsl:template match="nNumber">
<xsl:if test="@id=$search">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<!-- Convert the ID value from an attribute to an element under the nNumber node. -->
<xsl:template match="@id">
<xsl:element name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:include href="ApplyOrder.xsl"/>最后一行<xsl:include>在符号标记上运行一系列匹配以应用预定的顺序值(下面的示例)
<xsl:template match="symbol[@href='file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/CE0197_def.ai']" >
<xsl:copy>
<xsl:attribute name="order">111</xsl:attribute><xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>拼图的最后一部分是根据符号元素的@order值(下面的示例)对符号元素进行排序。我学到了一种很难的方法,那就是你不能在同一个操作中应用这个值并对应用值进行排序。
<symbol order="111" href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/CE0197_def.ai"/>
<symbol order="145" href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/IP68_def.ai"/>
<symbol order="171" href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/Humidity0-90_def.ai"/>
<symbol order="172" href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/Temp-10-55_def.ai"/>
<symbol order="181" href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/Bin_2_def.ai"/>
<symbol order="191" href="file:///U:/Labelling/Labels/Common Symbols/symbols with definitions/Recycle_def.ai"/>下面是我尝试将Michael的多阶段处理的例子引入到我的当前场景中。
我觉得我在这里错过了一些非常重要和基本的东西--但我完全忘记了它是什么.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:param name="search"></xsl:param>
<!-- Identity transform - copies all elements and attributes. -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="phase-1"/>
</xsl:copy>
</xsl:template>
<!-- Filter nNumber nodes by ID. -->
<xsl:template match="nNumber">
<xsl:if test="@id=$search">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="phase-1"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<!-- Convert the ID value from an attribute to an element under the nNumber node. -->
<xsl:template match="@id">
<xsl:element name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:include href="ApplyOrder.xsl"/>
<xsl:template match="/">
<xsl:variable name="phase-1-result">
<xsl:apply-templates select="/" mode="phase-1"/>
</xsl:variable>
<xsl:apply-templates select="$phase-1-result" mode="phase-2"/>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*" mode="phase-2">
<xsl:sort select="."/>
</xsl:apply-templates>
<xsl:apply-templates select="node()" mode="phase-2">
<xsl:sort select="@order" data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
转换的结果给出了完全不同的结果(标记不存在,文本不是来自相关节点等等)。我很乐意发表一个样本,如果有人认为它会有帮助(我意识到我已经占据了足够多的屏幕房地产)
注意:此工作流用于使用InDesign进行XML发布。然而,转换将运行在氧气v14。
发布于 2013-10-15 05:19:12
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:param name="search"></xsl:param>
<!-- Identity transform - copies all elements and attributes. -->
<xsl:template match="@*|node()" mode="phase-1">
<!-- ---------------------- ^^^^^^^^^^^^^^ -->
</xsl:template>
<!-- ... --->
<xsl:template match="node()|@*" mode="phase-2">
<!-- ---------------------- ^^^^^^^^^^^^^^ -->
</xsl:template>
</xsl:stylesheet>备注
<xsl:apply-templates>中提到了模板模式,但忘记了在实际模板上声明它们。https://stackoverflow.com/questions/19372418
复制相似问题