必须从XML中删除没有子元素的元素

时间:2016-07-17 10:19:25

标签: xml xslt xslt-2.0

我们输入XML。因为,Loop元素是StockLine标签。但是当它重复时它也会在Comment Line标签内创建空的子元素注释。

假设我们有2个stocklines。我们有2个commentLine标签。这两个commentLine标签未使用。想要从XML中删除。但同时不应删除带注释的CommentLine。只应删除没有评论的CommentLine。

基本上,我们想删除这部分:

<CommentLine>
            <OrderLineID>OR-1593605</OrderLineID>
         </CommentLine>

INPUT XML:

<?xml version="1.0" encoding="Windows-1252"?>
<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
             xsd:noNamespaceSchemaLocation="SDOC.XSD">
   <Orders>
      <OrderHeader>
      <Customer>0TARGT</Customer>
     </OrderHeader>
      <OrderDetails>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>xyz</StockCode>
            <StockDescription>xyz desc</StockDescription>
            <OrderQty>10.0</OrderQty>
            <OrderUom>CS</OrderUom>
            <Price>93.48</Price>
            <PriceUom>CS</PriceUom>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <UserDefined>1</UserDefined>
            <OrderLineID>OR-1593605</OrderLineID>
         </StockLine>
         <CommentLine>
            <OrderLineID>OR-1593605</OrderLineID>
         </CommentLine>
         <CommentLine>
            <Comment>TEST PURPOSE</Comment>
            <OrderLineID>OR-1810120</OrderLineID>
         </CommentLine>
         <CommentLine>
            <Comment>This is for test purpose</Comment>
            <OrderLineID>OR-1810121</OrderLineID>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-TEST</Comment>
            <OrderLineID>OR-1810123</OrderLineID>
         </CommentLine>
      </OrderDetails>
   </Orders>
</SalesOrders>

我们尝试过遵循XSLT:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="Windows-1252" indent="yes"/>

        <xsl:template match="@xsi:nil[.='true']" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    <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="Windows-1252"?>
    <SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
                 xsd:noNamespaceSchemaLocation="SDOC.XSD">
       <Orders>
          <OrderHeader>
          <Customer>0TARGT</Customer>
         </OrderHeader>
          <OrderDetails>
             <StockLine>
                <CustomerPoLine>9999</CustomerPoLine>
                <StockCode>xyz</StockCode>
                <StockDescription>xyz desc</StockDescription>
                <OrderQty>10.0</OrderQty>
                <OrderUom>CS</OrderUom>
                <Price>93.48</Price>
                <PriceUom>CS</PriceUom>
                <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
                <UserDefined>1</UserDefined>
                <OrderLineID>OR-1593605</OrderLineID>
             </StockLine>
             <CommentLine>
                <Comment>TEST PURPOSE</Comment>
                <OrderLineID>OR-1810120</OrderLineID>
             </CommentLine>
             <CommentLine>
                <Comment>This is for test purpose</Comment>
                <OrderLineID>OR-1810121</OrderLineID>
             </CommentLine>
             <CommentLine>
                <Comment>EDI-TEST</Comment>
                <OrderLineID>OR-1810123</OrderLineID>
             </CommentLine>
          </OrderDetails>
       </Orders>
    </SalesOrders>

1 个答案:

答案 0 :(得分:2)

如果您要删除所有没有CommentLine孩子的Comment元素,请添加模板<xsl:template match="CommentLine[not(Comment)]"/>

如果您只想删除CommentLine兄弟中OrderLineID的{​​{1}},请添加模板StockLine