XSLT to remove an Element's Value

前端 未结 2 954
误落风尘
误落风尘 2020-12-21 07:47

I need to remove a value from an element, but preserve the element itself in the output XML as an empty element.

My input file:


    

        
相关标签:
2条回答
  • 2020-12-21 08:14

    XSLT 1.0

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="c">
        <c/>
      </xsl:template>
    
    </xsl:stylesheet>
    

    XML Output

    <a>
       <b>TEXT1
        <c/>
          <d>qwe</d>
          <e>rty</e>
       </b>
       <b>TEXT2
        <c/>
          <d>iop</d>
          <e>jkl</e>
       </b>
    </a>
    

    Note: <c/> and <c></c> are equivalent.

    0 讨论(0)
  • 2020-12-21 08:14

    Even simpler/shorter:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="c/text()"/>
    </xsl:stylesheet>
    
    0 讨论(0)
提交回复
热议问题