2

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”,并且该值也保留在输出中。但是上面的评论不见了。我也想在输出中保留相同的评论。我怎样才能做到这一点?

4

1 回答 1

1

您必须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>
于 2013-07-17T16:21:06.553 回答