In case of Child
s with duplicate values in Identifier
element I would like to create new element create_duplicate
, but I do no want to match against current node itself. Currently //Identifier/text()
matches also to itself. Is there a way to exclude it and only match against all other Child/Identifier
s?
XML:
<Document>
<List>
<Child>
<Identifier>19961202</Identifier>
</Child>
<Child>
<Identifier>19961203</Identifier>
</Child>
</List>
<WebResponse>
<Product>
<WebIdentifier>19961202</WebIdentifier>
</Product>
<Product>
<WebIdentifier>19961203</WebIdentifier>
</Product>
</WebResponse>
</Document>
XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"></xsl:output>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Child">
<xsl:copy>
<xsl:apply-templates select="@* | node()"></xsl:apply-templates>
<operation>
<xsl:choose>
<xsl:when test="Identifier/text() = //Identifier/text()">
<xsl:value-of select="'create_duplicate'"/>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="Identifier/text() = //WebIdentifier/text()">
<xsl:value-of select="'update'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'create'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</operation>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Result:
<Document>
<List>
<Child>
<Identifier>19961202</Identifier>
<!-- I would expect 'update' here as it should skip itself-->
<operation>create_duplicate</operation>
</Child>
<Child>
<Identifier>19961203</Identifier>
<!-- I would expect 'update' here as it should skip itself-->
<operation>create_duplicate</operation>
</Child>
</List>
<WebResponse>
<Product>
<WebIdentifier>19961202</WebIdentifier>
</Product>
<Product>
<WebIdentifier>19961203</WebIdentifier>
</Product>
</WebResponse>
</Document>
CodePudding user response:
I guess you can group and then check if there is a cross-reference or not with a key:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:template match="Document/List">
<xsl:copy>
<xsl:for-each-group select="Child" group-by="Identifier">
<xsl:apply-templates select="current-group()"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="Child[key('webid', Identifier)]">
<xsl:copy>
<operation>{if (position() eq 1) then 'update' else 'duplicate'}</operation>
</xsl:copy>
</xsl:template>
<xsl:template match="Child[not(key('webid', Identifier))]">
<xsl:copy>
<operation>{if (position() eq 1) then 'create' else 'duplicate'}</operation>
</xsl:copy>
</xsl:template>
<xsl:output method="xml" indent="yes"/>
<xsl:key name="webid" match="Product" use="WebIdentifier"/>
<xsl:mode on-no-match="shallow-copy"/>
</xsl:stylesheet>
Or use two keys, one for the "grouping", the second for the cross-reference check:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:key name="id" match="Child" use="Identifier"/>
<xsl:template match="Child[. is key('id', Identifier)[1]][key('webid', Identifier)]">
<xsl:copy>
<operation>update</operation>
</xsl:copy>
</xsl:template>
<xsl:template match="Child[not(. is key('id', Identifier)[1])][key('webid', Identifier)]">
<xsl:copy>
<operation>duplicate</operation>
</xsl:copy>
</xsl:template>
<xsl:template match="Child[. is key('id', Identifier)[1]][not(key('webid', Identifier))]">
<xsl:copy>
<operation>create</operation>
</xsl:copy>
</xsl:template>
<xsl:template match="Child[not(. is key('id', Identifier)[1])][not(key('webid', Identifier))]">
<xsl:copy>
<operation>duplicate</operation>
</xsl:copy>
</xsl:template>
<xsl:output method="xml" indent="yes"/>
<xsl:key name="webid" match="Product" use="WebIdentifier"/>
<xsl:mode on-no-match="shallow-copy"/>