XSLT从创建的输出中删除空节点,并在变量中删除输出集

时间:2013-05-27 07:36:53

标签: xslt

您能否帮助从创建的输出和变量中的输出集中删除空节点。 我创建了xslt如下

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:user="http://www.contoso.com"
                version="1.0">
  <xsl:variable name="books">
    <header>
    <book author="Michael Howard">Writing Secure Code</book>
    <book author="Michael Kay">XSLT Reference</book>
    </header>
    <item>
      <item1>item1</item1>
      <item2></item2>//remove this empty tag
      <item3></item3>//remove this empty tag
    </item>
    <summary>
      <summary1>
      <sum1>SUB1</sum1>
        <sum2></sum2> //remove this empty tag
      </summary1>
      <summary2>Summary2</summary2>
      <summary3>Summ3</summary3>
    </summary>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:value-of select="$books"/>    
      <xsl:for-each select="msxsl:node-set($books)/node()">                
            <xsl:apply-templates/>
            <!--select="ms:node-set($completeDocument[1])/node()"/>-->           
      </xsl:for-each>  
  </xsl:template>
  <xsl:template match= "*[not(@*|*|comment()|processing-instruction()) and normalize-space()='']"/>
</xsl:stylesheet>

输出已生成

 <?xml version="1.0" encoding="utf-8"?>Writing Secure CodeXSLT Referenceitem1item2SUB1sub2Summary2Summ3Writing Secure CodeXSLT Referenceitem1item2SUB1sub2Summary2Summ3

预期输出

 <header>
        <book author="Michael Howard">Writing Secure Code</book>
        <book author="Michael Kay">XSLT Reference</book>
        </header>
        <item>
            <item1>item1</item1> 
        </item>
        <summary>
          <summary1>
          <sum1>SUB1</sum1>
          </summary1>
          <summary2>Summary2</summary2>
          <summary3>Summ3</summary3>
        </summary>

预期输出显示删除空节点。

1 个答案:

答案 0 :(得分:1)

您已经编写了一个模板,可以确保不复制某些元素,但为了使该方法起作用,您需要添加身份转换模板,该模板复制您要复制的节点并继续处理。否则,只有内置模板不复制元素节点,只是处理子节点和输出文本节点。

所以你想要像

这样的东西
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:user="http://www.contoso.com"
                exclude-result-prefixes="xsl msxsl user"
                version="1.0">
  <xsl:variable name="books">
    <header>
    <book author="Michael Howard">Writing Secure Code</book>
    <book author="Michael Kay">XSLT Reference</book>
    </header>
    <item>
      <item1>item1</item1>
      <item2></item2>//remove this empty tag
      <item3></item3>//remove this empty tag
    </item>
    <summary>
      <summary1>
      <sum1>SUB1</sum1>
        <sum2></sum2> //remove this empty tag
      </summary1>
      <summary2>Summary2</summary2>
      <summary3>Summ3</summary3>
    </summary>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:apply-templates select="msxsl:node-set($books)/node()"/>
  </xsl:template>

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

  <xsl:template match= "*[not(@*|*|comment()|processing-instruction()) and normalize-space()='']"/>
</xsl:stylesheet>