建议,如何保留注释的位置与输入文件中的相同,同时将其他同级元素的元素重新排序到注释。这里不需要元素之间的标点符号,只有一些元素将被移动为给定的XSLT(版本2)。
目前所有评论都出现在所有元素的末尾(最后给出的apply-templates for comment)。请建议获得下面给出的所需输出。
XML:
<article>
<bm>
<ref id="ref1">
<au>Rudramuni TP</au>. (<year>2014</year>). <jtitle>Sun Family</jtitle>. <articleTitle>The Solar System</articleTitle>.<!--The comment related article--> <iss>2</iss>, <vol>11</vol>: <!--The comment related to volume --><fpage>1</fpage>-<lpage>12</lpage>
</ref>
<ref id="ref2">
<au>Kishan TR</au>. (<year>2014</year>). <jtitle>Galxy</jtitle>. <articleTitle>The Galxy<!--The comment related to title --></articleTitle>. <iss>2</iss>, <vol>11</vol>: <fpage>1</fpage>-<!--The comment related to pages --><lpage>12</lpage>
</ref>
</bm>
</article>
XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="ref">
<xsl:element name="ref">
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="au"/>
<xsl:apply-templates select="articleTitle"/>
<xsl:apply-templates select="jtitle"/>
<xsl:apply-templates select="year"/>
<xsl:apply-templates select="vol"/>
<xsl:apply-templates select="iss"/>
<xsl:apply-templates select="fpage"/>
<xsl:apply-templates select="lpage"/>
<xsl:apply-templates select="comment()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
必填结果:
<article>
<bm>
<ref id="ref1"><au>Rudramuni TP</au>
<articleTitle>The Solar System</articleTitle><!--The comment related article--><jtitle>Sun Family</jtitle>
<year>2014</year><vol>11</vol><!--The comment related to volume --><iss>2</iss>
<fpage>1</fpage><lpage>12</lpage>
</ref>
<ref id="ref2"><au>Kishan TR</au>
<articleTitle>The Galxy<!--The comment related to title --></articleTitle><jtitle>Galxy</jtitle>
<year>2014</year><vol>11</vol><iss>2</iss>
<fpage>1</fpage><!--The comment related to pages --><lpage>12</lpage>
</ref>
</bm>
</article>
答案 0 :(得分:1)
怎么样:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="comment" match="comment()" use="generate-id(preceding-sibling::*[1])" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ref">
<ref>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="au"/>
<xsl:apply-templates select="articleTitle"/>
<xsl:apply-templates select="jtitle"/>
<xsl:apply-templates select="year"/>
<xsl:apply-templates select="vol"/>
<xsl:apply-templates select="iss"/>
<xsl:apply-templates select="fpage"/>
<xsl:apply-templates select="lpage"/>
</ref>
</xsl:template>
<xsl:template match="ref/*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:copy-of select="key('comment', generate-id())"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>