0

您能否帮助从创建的输出和变量中设置的输出中删除空节点。我创建了 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>

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

4

1 回答 1

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>
于 2013-05-27T09:06:41.557 回答