我在下面的表单中有xml
内容,其中附录可以包含任何内容。子女也是如此。
<topicref outputclass="Dx:Appendix" href="AppendixA-test.dita">
<topicref outputclass="Dx:Appendix" href="AppendixA-sub-test.dita"/>
</topicref>
<topicref outputclass="Dx:Appendix" href="AppendixB-test.dita"/>
现在我希望输出xml 看起来像这样:
<appendix href="AppendixA-test.dita">
<topicref href="AppendixA-sub-test.dita"/>
</appendix>
<appendix href="AppendixB-test.dita"/>
答案 0 :(得分:1)
这应该有效:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<!--
Convert first-level <topicref> elements to <appendix> and copy the attributes,
but ignore the @outputclass attribute
-->
<xsl:template match="topicref[contains(@outputclass, 'Dx:Appendix')][not(parent::topicref)]">
<appendix>
<xsl:apply-templates select="@*[name()!='outputclass'] | node()"/>
</appendix>
</xsl:template>
<!--
Copy all elements and attributes, except the @outputclass attribute
(default copy template)
-->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*[name()!='outputclass'] | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
这是一个XSLT-1.0解决方案:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- copy all nodes except those more specific -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<!-- modify all "topicref" nodes except those more specific -->
<xsl:template match="topicref">
<xsl:element name="appendix">
<xsl:apply-templates select="node()|@href" />
</xsl:element>
</xsl:template>
<!-- do not modify the names of "topicref" elements with "topicref" parents" -->
<xsl:template match="topicref[parent::topicref]">
<xsl:copy>
<xsl:apply-templates select="node()|@href" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>