消除xslt 1.0中的重复项

问题描述 投票:0回答:3

我是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>
xslt soa
3个回答
-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>

-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
投票
<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
© www.soinside.com 2019 - 2024. All rights reserved.