Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT:如何删除XML中的重复标记_Xml_Xslt_Xslt 2.0 - Fatal编程技术网

XSLT:如何删除XML中的重复标记

XSLT:如何删除XML中的重复标记,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我们在这个输入XML中得到了重复的标记。我们希望使用XSLT2.0模板指令删除此重复 输入XML: <?xml version="1.0" encoding="UTF-8"?> <SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SDOC.XSD"> <Orders> <OrderHeader&

我们在这个输入XML中得到了重复的标记。我们希望使用XSLT2.0模板指令删除此重复

输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SDOC.XSD">
   <Orders>
      <OrderHeader>
         <CustomerPoNumber>AB-54354</CustomerPoNumber>
      </OrderHeader>
      <OrderDetails>
         <CommentLine>
            <Comment>Ensure saddle is color coded</Comment>
            <OrderLineID>OR-1810127</OrderLineID>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-001</Comment>
            <OrderLineID>OR-1810128</OrderLineID>
         </CommentLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>ABSH-SMH-12OZ-01</StockCode>
            <StockDescription>SMH ABS BALANCE SHAMPOO 12OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
         </StockLine>
         <CommentLine>
            <Comment>This is for test purpose</Comment>
            <OrderLineID>OR-1810124</OrderLineID>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-SAVE</Comment>
            <OrderLineID>OR-1810125</OrderLineID>
         </CommentLine>
         <CommentLine>
            <Comment>Ensure saddle is color coded</Comment>
            <OrderLineID>OR-1810127</OrderLineID>
         </CommentLine>
      </OrderDetails>
   </Orders>
</SalesOrders>

AB-54354
确保鞍座有颜色编码
OR-1810127
EDI-001
OR-1810128
9999
ABSH-SMH-12OZ-01
SMH ABS平衡香波12盎司
1
这是为了测试
OR-1810124
EDI-SAVE
OR-1810125
确保鞍座有颜色编码
OR-1810127
我们尝试在其上使用以下XSLT:

<?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 match="@*|node()">
      <xsl:copy copy-namespaces="no">
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="StockLine[not(StockCodeDescription) and not (OrderQty) and not(Price)]">
      <CommentLine>
         <Comment>
            <xsl:value-of select="StockCode" />
         </Comment>
         <xsl:copy-of select="OrderLineID" />
      </CommentLine>
   </xsl:template>
   <xsl:template match="CommentLine[OrderLineID = preceding-sibling::StockLine/OrderLineID and not(Comment)]" />
</xsl:stylesheet>

预期输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SDOC.XSD">
   <Orders>
      <OrderHeader>
         <CustomerPoNumber>AB-54354</CustomerPoNumber>
      </OrderHeader>
      <OrderDetails>
         <CommentLine>
            <Comment>Ensure saddle is color coded</Comment>
            <OrderLineID>OR-1810127</OrderLineID>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-001</Comment>
            <OrderLineID>OR-1810128</OrderLineID>
         </CommentLine>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <StockCode>ABSH-SMH-12OZ-01</StockCode>
            <StockDescription>SMH ABS BALANCE SHAMPOO 12OZ</StockDescription>
            <OrderQty>1.0</OrderQty>
         </StockLine>
         <CommentLine>
            <Comment>This is for test purpose</Comment>
            <OrderLineID>OR-1810124</OrderLineID>
         </CommentLine>
         <CommentLine>
            <Comment>EDI-SAVE</Comment>
            <OrderLineID>OR-1810125</OrderLineID>
         </CommentLine>
      </OrderDetails>
   </Orders>
</SalesOrders>

AB-54354
确保鞍座有颜色编码
OR-1810127
EDI-001
OR-1810128
9999
ABSH-SMH-12OZ-01
SMH ABS平衡香波12盎司
1
这是为了测试
OR-1810124
EDI-SAVE
OR-1810125
此元素在XML中重复

<CommentLine>
            <Comment>Ensure saddle is color coded</Comment>  
            <OrderLineID>OR-1810127</OrderLineID>  
          </CommentLine>  

确保鞍座有颜色编码
OR-1810127

任何帮助都将不胜感激

如果您只想消除
CommentLine
元素的完全重复项,请使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

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

    <xsl:template match="CommentLine[some $sib in preceding-sibling::CommentLine satisfies deep-equal(., $sib)]"/>
</xsl:stylesheet>

如果您只想消除
CommentLine
元素的完全重复,请使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

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

    <xsl:template match="CommentLine[some $sib in preceding-sibling::CommentLine satisfies deep-equal(., $sib)]"/>
</xsl:stylesheet>


当我应用模板时。它正在删除副本。但额外的空标签仍然存在。输出变成这样,那个输出有什么问题?这似乎是你所描述的预期输出,除非我遗漏了什么。对我来说很有用。这里没有额外的
元素(Oxygen/XML,Saxon EE 9.4.0.6)。我要寻找的所有东西的输出都来了,但另外它创建了空标记。当我与预期输出进行比较时,仅此标记存在差异。当我应用模板时。它正在删除副本。但额外的空标签仍然存在。输出变成这样,那个输出有什么问题?这似乎是你所描述的预期输出,除非我遗漏了什么。对我来说很有用。这里没有额外的
元素(Oxygen/XML,Saxon EE 9.4.0.6)。我要寻找的所有东西的输出都来了,但另外它创建了空标记。当我与预期输出进行比较时,仅此标记存在差异。