2016-08-03 67 views
0

我们有输入XML。我们试图去除那些有空值和非空值的元素。我们有<Item>作为重复元素。 <TermsCode>对于重复项目具有空值和非空值。XSLT:检查值是否为空然后删除标记

我们必须在XSLT中检查后删除这样的<TermsCode>空标签如果它是空的。或者如果它有价值,它应该保留标签。 同样,我们正在尝试为每个元素写入项目节点。如果它是空的,然后删除。如果不是,那么应该将标签保留在Output XML中。

INPUT XML

<?xml version="1.0" encoding="UTF-8"?> 
<SetupArCustomer> 
    <Item> 
     <Key> 
     <Customer>0039069</Customer> 
     </Key> 
     <Name>ABC SOLUTIONS LLC</Name> 
     <CreditLimit>0.0</CreditLimit> 
     <PriceCode>WH</PriceCode> 
     <Branch>NY</Branch> 
     <TermsCode>00</TermsCode> 
     </Item> 
    <Item> 
     <Key> 
     <Customer>0039070</Customer> 
     </Key> 
     <Name>CCD WHOLESALE NY INC.</Name> 
     <CreditLimit>0.0</CreditLimit> 
     <PriceCode>HY</PriceCode> 
     <Branch>NY</Branch> 
     <TermsCode/> 
    </Item> 
    </SetupArCustomer> 

受审XSLT2.0

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" encoding="Windows-1252" indent="yes" /> 
    <xsl:template xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" match="@xsi:nil[.='true']" /> 
    <xsl:template match="@*|node()"> 
    <xsl:copy copy-namespaces="no"> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

期望输出

<?xml version="1.0" encoding="UTF-8"?> 
    <SetupArCustomer> 
     <Item> 
      <Key> 
      <Customer>0039069</Customer> 
      </Key> 
      <Name>ABC SOLUTIONS LLC</Name> 
      <CreditLimit>0.0</CreditLimit> 
      <PriceCode>WH</PriceCode> 
      <Branch>NY</Branch> 
      <TermsCode>00</TermsCode> 
      </Item> 
     <Item> 
      <Key> 
      <Customer>0039070</Customer> 
      </Key> 
      <Name>CCD WHOLESALE NY INC.</Name> 
      <CreditLimit>0.0</CreditLimit> 
      <PriceCode>HY</PriceCode> 
      <Branch>NY</Branch> 
     </Item> 
     </SetupArCustomer> 

回答

1

要删除EMP TY TermsCode元素:

<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="TermsCode[not(node())]"/> 

</xsl:stylesheet> 

要删除Item任何空的子元素,更改:

<xsl:template match="TermsCode[not(node())]"/> 

到:

<xsl:template match="Item/*[not(node())]"/> 
+0

感谢。我应用于env 。它正在为我们工作。 – NEO