Я новичок в 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>
Начните здесь: jenitennison.com/xslt/grouping/muenchian.html
<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
Я могу использовать только xslt 1.0
С 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>
Плохой метод - см. здесь, почему: jenitennison.com/xslt/grouping/muenchian.html
Вы можете добавить имя к шаблону копирования, а затем вызвать этот шаблон внутри вашего теста if.
<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
Вы уверены? (xsltfiddle.liberty-development.net/ncdD7mj/1) Может быть, вы имели в виду что-то вроде этого? (xsltfiddle.liberty-development.net/ncdD7mj/2)
Извините, сэр, я забыл изменить (xpath-default-namespace) пространство имен по умолчанию, так как это не разрешено в xslt 1.0.
Возможный дубликат Удаление дубликатов в xsl