Update XML Parent Element Attribute Using XSLT
I hope someone can help, I can't figure this out, maybe it just can't be
done.
I have the following XML and need to update RedressNumber and
KnownTravelerNumber based on a condition of Document elements. I am using
the following XSLT but it's not working.
The attribute should be copied as is if DocTypeCode condition is not true.
The attribute value should be replaced with DocID value if condition is
true.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="identity.xsl" />
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"
encoding="UTF-8" />
<xsl:template match="ProfileRead">
<xsl:apply-imports />
</xsl:template>
<xsl:template match="ProfileRead/Profile/Traveler/Customer">
<xsl:copy>
<xsl:for-each select="Document">
<xsl:if test="@DocTypeCode = 'KTID'">
<xsl:attribute name="KnownTravelerNumber">
<xsl:value-of select="@DocID"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@DocTypeCode = 'RDNR'">
<xsl:attribute name="RedressNumber">
<xsl:value-of select="@DocID"/>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template
match="ProfileRead/Profile/Traveler/Customer/@KnownTravelerNumber" />
<xsl:template
match="ProfileRead/Profile/Traveler/Customer/@RedressNumber" />
<!-- remove element -->
<xsl:template
match="ProfileRead/Profile/Traveler/Customer/Document[@DocTypeCode='KTID']"
/>
<xsl:template
match="ProfileRead/Profile/Traveler/Customer/Document[@DocTypeCode='RDNR']"
/>
</xsl:stylesheet>
<ProfileRead>
<Profile>
<Traveler>
<Customer GenderCode="M" RedressNumber="321"
KnownTravelerNumber="123">
<PersonName LanguageIDCode="EN-US">
<GivenName>John</GivenName>
<MiddleName>Long</MiddleName>
<SurName>Smith</SurName>
<NameSuffix>Junior</NameSuffix>
</PersonName>
<Document DocID="666" DocTypeCode="RDNR" />
<Document DocID="111" DocTypeCode="KAMAL" />
<Document DocID="222" DocTypeCode="FRANK" />
</Customer>
</Traveler>
</Profile>
</ProfileRead>
No comments:
Post a Comment