我有以下xml
<xml>
<object context="3-cumulative" >
<metadata>
<ref cite="4.2" relevance="first.2"/>
</metadata>
<body>
<para>
<text>
applicable on and after December 14,2007.
</text>
</para>
</body>
</object>
<object context="3-cumulative" >
<metadata>
<ref cite="4.2" relevance="first.1"/>
</metadata>
<body>
<para>
<text>
applicable on and after December 14,2006.
</text>
</para>
</body>
</object>
<object context="3-cumulative" >
<metadata>
<related-content-ref cite="5 annuity" relevance="first.1"/>
</metadata>
<body>
<para>
<text>
applicable on and after December 14, 2008
</text>
</para>
</body>
</object>
<mainbody>
<num cite="4.2">4.2</num>
<heading>Stock exchanges</heading>
<prov-body>
<text>
Notwithstanding the provisions of a convention ... as defined in the
<italic>Income Tax Act</italic>.
</text>
<prov>
<num cite="5 annuity"/>
<heading>“annuity”</heading>
<text>
<term>“annuity”</term>does not include any pension payment ...
</text>
<text>
any pension payment ...
</text>
</prov>
</prov-body>
</mainbody>
</xml>
我需要查看是否在“mainbody”num / @ cite中找到了任何object / metadata / ref / @ cite,然后来自对象的para / text应该复制到第一个Text节点的末尾,并且应该按对象排序/元数据/ REF / @相关性。
输出应为:
<xml>
<mainbody>
<num cite="4.2">4.2</num>
<heading>Stock exchanges</heading>
<prov-body>
<text>
Notwithstanding the provisions of a convention ... as defined in the
<italic>Income Tax Act</italic>.
**applicable on and after December 14, 2006**
**applicable on and after December 14, 2007**
</text>
<prov>
<num cite="5 annuity"/>
<heading>“annuity”</heading>
<text>
<term>“annuity”</term>does not include any pension payment ...
**applicable on and after December 14, 2008**
</text>
<text>
any pension payment ...
</text>
</prov>
</prov-body>
</mainbody>
</xml>
答案 0 :(得分:2)
此XSLT 1.0转换(如果version
属性更改为2.0并且使用XSLT 2.0处理器运行转换,则会产生相同的结果):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kRefByCite"
match="metadata/*" use="@cite" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"text[(preceding::text[1]|preceding::num[1])
[last()]
[key('kRefByCite', @cite)]]">
<text>
<xsl:apply-templates/>
<xsl:for-each select=
"key('kRefByCite', preceding::num[1]/@cite)">
<xsl:sort select="@relevance"/>
<xsl:value-of select="../../body/para/text"/>
</xsl:for-each>
</text>
</xsl:template>
<xsl:template match=
"node()
[parent::* and not(ancestor-or-self::mainbody)]"/>
</xsl:stylesheet>
应用于提供的XML文档时:
<xml>
<object context="3-cumulative" >
<metadata>
<ref cite="4.2" relevance="first.2"/>
</metadata>
<body>
<para>
<text>
applicable on and after December 14,2007.
</text>
</para>
</body>
</object>
<object context="3-cumulative" >
<metadata>
<ref cite="4.2" relevance="first.1"/>
</metadata>
<body>
<para>
<text>
applicable on and after December 14,2006.
</text>
</para>
</body>
</object>
<object context="3-cumulative" >
<metadata>
<related-content-ref cite="5 annuity" relevance="first.1"/>
</metadata>
<body>
<para>
<text>
applicable on and after December 14, 2008
</text>
</para>
</body>
</object>
<mainbody>
<num cite="4.2">4.2</num>
<heading>Stock exchanges</heading>
<prov-body>
<text>
Notwithstanding the provisions of a convention ... as defined in the
<italic>Income Tax Act</italic>.
</text>
<prov>
<num cite="5 annuity"/>
<heading>“annuity”</heading>
<text>
<term>“annuity”</term>does not include any pension payment ...
</text>
<text>
any pension payment ...
</text>
</prov>
</prov-body>
</mainbody>
</xml>
会产生想要的正确结果:
<xml>
<mainbody>
<num cite="4.2">4.2</num>
<heading>Stock exchanges</heading>
<prov-body>
<text>
Notwithstanding the provisions of a convention ... as defined in the
<italic>Income Tax Act</italic>.
applicable on and after December 14,2006.
applicable on and after December 14,2007.
</text>
<prov>
<num cite="5 annuity"/>
<heading>“annuity”</heading>
<text>
<term>“annuity”</term>does not include any pension payment ...
applicable on and after December 14, 2008
</text>
<text>
any pension payment ...
</text>
</prov>
</prov-body>
</mainbody>
</xml>
<强>解释强>:
适当覆盖 identity rule 并使用 xsl:key
和 key()
功能。