XML
<?xml-stylesheet type="text/xsl" href="script.xsl"?>
<elements>
<stack>
<name>Ray</name>
<status>0</status>
</stack>
<!-- Comment 1 -->
<things>
<!-- Comment 2 -->
<thing>
<color>red</color>
<!-- State Comment -->
<state>solid</state>
<!-- Weight Comment -->
<weight>45</weight>
<unit>34</unit>
<!-- Comment 3 -->
</thing>
</things>
<favs>
<stick>ready</stick>
<!-- Comment 4-->
</favs>
</elements>
预期产出
<elements>
<stack>
<name>Ray</name>
<status>0</status>
</stack>
<!-- Comment 1 -->
<mainElements>
<!-- Comment 2 -->
<specialThing>
<!-- Weight Comment -->
<PropertyOne>45</PropertyOne>
<PropertyTwo>red</PropertyTwo>
<!-- State Comment -->
<PropertyThree>solid</PropertyThree>
</specialThing>
<!-- Comment 3 -->
</mainElements>
<favs>
<stick>ready</stick>
<!-- Comment 4-->
</favs>
</elements>
当前 XSL
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="things">
<mainElements>
<xsl:apply-templates select="thing"/>
</mainElements>
</xsl:template>
<xsl:template match="thing">
<specialThing>
<xsl:apply-templates select="weight"/>
<xsl:apply-templates select="color"/>
<xsl:apply-templates select="state"/>
</specialThing>
</xsl:template>
<xsl:template match="weight">
<PropertyOne>
<xsl:value-of select="."/>
</PropertyOne>
</xsl:template>
<xsl:template match="color">
<PropertyTwo>
<xsl:value-of select="."/>
</PropertyTwo>
</xsl:template>
<xsl:template match="state">
<PropertyThree>
<xsl:value-of select="."/>
</PropertyThree>
</xsl:template>
</xsl:stylesheet>
正如我在输出中提到的那样,我无法正确缩进输出。
但我想要实现的主要目标是维护输出中维护的标签的注释。
例如:名为weight 的标签被重命名为“PropertyOne”,并且该值也保留在输出中。但上面的评论丢失了。我也想在输出中保留相同的注释。我怎样才能做到这一点?
你必须做一个xsl:apply-templates
对于前一个兄弟姐妹comment()
的。由于您要更改顺序weight
/color
/state
,您必须将其添加到他们的每个模板中。
还有,匹配Comment 3
这很棘手,我所做的可能不适用于您的实际数据。
XML输入
<elements>
<stack>
<name>Ray</name>
<status>0</status>
</stack>
<!-- Comment 1 -->
<things>
<!-- Comment 2 -->
<thing>
<color>red</color>
<!-- State Comment -->
<state>solid</state>
<!-- Weight Comment 1 -->
<!-- Weight Comment 2 -->
<weight>45</weight>
<unit>34</unit>
<!-- Comment 3 -->
</thing>
</things>
<favs>
<stick>ready</stick>
<!-- Comment 4-->
</favs>
</elements>
XSLT 1.0
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="things">
<mainElements>
<xsl:apply-templates select="thing|comment()"/>
</mainElements>
</xsl:template>
<xsl:template match="thing">
<specialThing>
<xsl:apply-templates select="weight"/>
<xsl:apply-templates select="color"/>
<xsl:apply-templates select="state"/>
</specialThing>
<xsl:apply-templates select="comment()[not(following-sibling::*)]"/>
</xsl:template>
<xsl:template match="weight">
<xsl:apply-templates select="preceding-sibling::comment()[generate-id(following-sibling::*[1])=generate-id(current())]"/>
<PropertyOne>
<xsl:value-of select="."/>
</PropertyOne>
</xsl:template>
<xsl:template match="color">
<xsl:apply-templates select="preceding-sibling::comment()[generate-id(following-sibling::*[1])=generate-id(current())]"/>
<PropertyTwo>
<xsl:value-of select="."/>
</PropertyTwo>
</xsl:template>
<xsl:template match="state">
<xsl:apply-templates select="preceding-sibling::comment()[generate-id(following-sibling::*[1])=generate-id(current())]"/>
<PropertyThree>
<xsl:value-of select="."/>
</PropertyThree>
</xsl:template>
</xsl:stylesheet>
XML输出
<elements>
<stack>
<name>Ray</name>
<status>0</status>
</stack>
<!-- Comment 1 -->
<mainElements>
<!-- Comment 2 -->
<specialThing>
<!-- Weight Comment 1 -->
<!-- Weight Comment 2 -->
<PropertyOne>45</PropertyOne>
<PropertyTwo>red</PropertyTwo>
<!-- State Comment -->
<PropertyThree>solid</PropertyThree>
</specialThing>
<!-- Comment 3 -->
</mainElements>
<favs>
<stick>ready</stick>
<!-- Comment 4-->
</favs>
</elements>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)