此样式表:
<xsl:stylesheet version="1.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="subsection">
<subsection label="{heading/@label}" name="{heading/line}">
<xsl:apply-templates select="@*|node()"/>
</subsection>
</xsl:template>
<xsl:template match="section">
<section name="{heading/line}">
<xsl:apply-templates select="@*|node()"/>
</section>
</xsl:template>
</xsl:stylesheet>
输出:
<section name="A Heading">
<heading>
<line absolutePage="4" page="2" num="35">A Heading</line>
</heading>
<subsection label="3" name="A Subheading" type="type1">
<heading label="3">
<line absolutePage="4" page="2" num="36">A Subheading</line>
</heading>
<content></content>
</subsection>
</section>
注释
:只要可以使用文本结果元素和属性值模板,就可以使用它。这使得代码紧凑和快速。如果你想要更一般的答案,请澄清。
编辑
错过了
section/@name
. 当然,如果是空字符串
section/@label
不用麻烦你,你可以用
section|subsection
模式匹配。