从 xslt 1.0 中消除重复项
Eliminating duplicates from xslt 1.0
我是 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>
<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.
使用 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>
<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
我是 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>
<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.
使用 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>
<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