从xslt 1.0中消除重复项

时间:2019-03-19 14:12:45

标签: xslt soa

我是xslt的新手,有谁可以帮助我

我尝试了几种方法,但是没有运气可以帮助您。

我只能使用XSLT 1.0

    <?xml version="1.0" encoding="UTF-8"?><OrderNumberVar>
       <VariableCollection xmlns="http://www.mcp.com/xsd" 
       xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" 
       xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:client="http://xmlns.oracle.com/MCB_SOA/JDE" 
   xmlns:tns="http://www.mcb.com/xsd">
      <tns:Variable>
         <tns:OrderNumber>156708</tns:OrderNumber>
      </tns:Variable>
      <tns:Variable>
         <tns:OrderNumber>156708</tns:OrderNumber>
      </tns:Variable>
      <tns:Variable>
         <tns:OrderNumber>263932</tns:OrderNumber>
      </tns:Variable>
   </VariableCollection>

需要消除上述xml中的重复内容

</VariableCollection>
      <tns:Variable>
         <tns:OrderNumber>156708</tns:OrderNumber>
      </tns:Variable>
      <tns:Variable>
         <tns:OrderNumber>263932</tns:OrderNumber>
      </tns:Variable>
</VariableCollection>

3 个答案:

答案 0 :(得分:-1)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xpath-default-namespace="http://www.mcp.com/xsd"
    xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:client="http://xmlns.oracle.com/MCB_SOA/JDE" 
    xmlns:tns="http://www.mcb.com/xsd"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="VariableCollection">
        <xsl:copy>
            <xsl:for-each-group select="tns:Variable" group-by="tns:OrderNumber">
                <tns:Variable>
                    <tns:OrderNumber><xsl:value-of select="current-grouping-key()"/></tns:OrderNumber>
                    </tns:Variable>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
use it.

答案 1 :(得分:-1)

使用XSLT 1.0,您可以执行以下操作,并使用preceding-sibling值进行检查:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns="http://www.mcp.com/xsd"
    xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:client="http://xmlns.oracle.com/MCB_SOA/JDE"
    xmlns:tns="http://www.mcb.com/xsd"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs plnk wsdl client tns xsl" version="1.0">

    <xsl:strip-space elements="*"/>

    <xsl:output indent="yes"/>

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

    <xsl:template match="tns:Variable">
        <xsl:variable name="current">
            <xsl:value-of select="tns:OrderNumber"/>
        </xsl:variable>
        <xsl:if test="not(preceding-sibling::tns:Variable[tns:OrderNumber=$current])">
            <tns:Variable>
                <tns:OrderNumber>
                    <xsl:value-of select="$current"/>
                </tns:OrderNumber>
            </tns:Variable>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

答案 2 :(得分:-1)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xpath-default-namespace="http://www.mcp.com/xsd"
    xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:client="http://xmlns.oracle.com/MCB_SOA/JDE" 
    xmlns:tns="http://www.mcb.com/xsd"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" indent="yes"/>
    <xsl:key name="uniq" match="tns:Variable" use="tns:OrderNumber"/>
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="VariableCollection">
        <xsl:copy>
            <xsl:for-each select="tns:Variable[ generate-id() = generate-id(key('uniq',tns:OrderNumber)[1])]">
                <tns:Variable><tns:OrderNumber><xsl:value-of select="key('uniq',tns:OrderNumber)[1]"/></tns:OrderNumber></tns:Variable>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
You can also do using xsl:key