xsl删除特定属性并将其添加为子节点

时间:2016-10-13 19:36:09

标签: xslt

我有以下xml:

<Envelope> 
  <ABC>
   <Hierarchy>
      <Family> 
       <History> ... </History>
      <Plan>  
       <Mom Name="Parent1">
         <Child Name = "Child11">
          <GrandChild Name="GC111" Label="" Sequence = "1">
            <Attributes>... </Attributes>
           </GrandChild>
          </Child>
         </Mom>
        <Child Name = "ChildIndependent1">
          <GrandChild Name="GCIndep12"   Label="&lt;Requested&gt;&lt;Item1&gt;68&lt;/Item1&gt;&lt;Item2&gt;69&lt;/Item2&g    t;&lt;/Requested&gt;" Sequence = "2" >
            <Attributes>... </Attributes>
        </GrandChild>       </Child>
  </Plan>      </Family>
   </Hierarchy>      </ABC>
</Envelope>

这是我当前在xml上创建的xsl,用于创建围绕独立子标签的虚拟妈妈标签。它还转换了&amp; LT;和&amp; GT;到&lt;和&gt;

         <xsl:stylesheet version="2.0" >
         <xsl:output method="xml" indent="yes" use-character-maps="angle-brackets"/>
           <xsl:character-map name="angle-brackets">
           <xsl:output-character character="&lt;" string="&lt;"/>
           <xsl:output-character character="&gt;" string="&gt;"/>
         </xsl:character-map>
        <xsl:template match="node()|@*">
       <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
     </xsl:template>
      <xsl:template match="//ABC/Hierarchy/Family">
        <xsl:element name="Plan">
           <xsl:for-each select="//ABC/Hierarchy/Family/Plan/*">
              <xsl:if test="self::Mom">
                   <xsl:copy-of select="."/>
              </xsl:if>
               <xsl:if test="self::Child">
                    <xsl:element name="Mom">
                       <xsl:attribute name="Name">dummy</xsl:attribute>
                         <xsl:copy-of select="."/>
                    </xsl:element>
               </xsl:if>
           </xsl:for-each>
       </xsl:element>
      </xsl:element>
       </xsl:template>
       </xsl:stylesheet>

我需要修改上面的xsl以创建LabelRequested标记,其中label属性的内容作为GrandChild的子项,并删除Label属性本身,如下所示,xsl已经转换了&amp; LT;和&amp; GT;到&lt;和&gt;。

        <Child Name = "ChildIndependent1">
          <GrandChild Name="GCIndep12"    Sequence = "2" >
             <LabelRequested>
                        <Requested><Item1>68</Item1><Item2>69</Item2>   </Requested>
             </LabelRequested>
            <Attributes>... </Attributes>
        </GrandChild>     

知道如何更改上面的xsl才能执行此操作吗?

1 个答案:

答案 0 :(得分:1)

要最小化手头问题的示例,请考虑以下样式表:

XSLT 2.0

<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="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="GrandChild">
    <xsl:copy>
        <xsl:apply-templates select="@* except @Label"/>
        <LabelRequested>
            <xsl:value-of select="@Label" disable-output-escaping="yes"/>
        </LabelRequested>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
   <ABC>
      <Hierarchy>
         <Family>
            <History> ... </History>
            <Plan>
               <Mom Name="Parent1">
                  <Child Name="Child11">
                     <GrandChild Name="GC111" Sequence="1">
                        <LabelRequested/>
                        <Attributes>... </Attributes>
                     </GrandChild>
                  </Child>
               </Mom>
               <Child Name="ChildIndependent1">
                  <GrandChild Name="GCIndep12" Sequence="2">
                     <LabelRequested><Requested><Item1>68</Item1><Item2>69</Item2></Requested></LabelRequested>
                     <Attributes>... </Attributes>
                  </GrandChild>
               </Child>
            </Plan>
         </Family>
      </Hierarchy>
   </ABC>
</Envelope>

请注意,这假设您的处理器支持disable-output-escaping并且处理链允许它执行它。否则,您必须使用XSLT 3.0或扩展函数来解析Label属性中包含的标记。