I am trying to update an element in below XML using data in secondary or different xml file. Both input XML and secondary XML file has equal number of segments. I need to take a value in first segment of secondary XML and update an element in INPUT xml and so on. I am not sure whether it can be done using XSL or not, can anyone guide me.
To be more specific, i am trying to update INPUT XML's <indicator></indicator>
value in each <iOSection>
based on secondary XML's //PDetails/PStatus/Code and //PDetails/PStatus/Description values.
Below is INPUT XML file:
<IResponse>
<iOSection>
<Details>
<Info>
<pNumber>FB061689</pNumber>
<indicator></indicator>
<Identifier>1</Identifier>
</Info>
</Details>
<Token>
<Reference>1UUYD05BHM21OJCK3881C7F</Reference>
</Token>
</iOSection>
<iOSection>
<Details>
<Info>
<pNumber>FB061690</pNumber>
<indicator></indicator>
<Identifier>2</Identifier>
</Info>
</Details>
<Token>
<Reference>1UUYD05BHM21OJCK3881C7F</Reference>
</Token>
</iOSection>
</IResponse>
below is secondary XML file - it is available in xsl variable called RSPDetails
<RS PartID="abcd" SysID="mnc">
<PDetails>
<PN>FB063586</PN>
<PStatus>
<Code>0</Code>
<Description>Cancelled</Description>
</PStatus>
</PDetails>
<PDetails>
<Error>
<Code>92</Code>
<Message>failed</Message>
</Error>
</PDetails>
</RS>
The value of <indicator>
should be 'YES' when //PDetails/PStatus/Code = '0' and //PDetails/PStatus/Description = 'Cancelled' , in all other cases it should be 'NO'
The condition should apply for <iOSection>
position 1 using <PDetails>
position 1 data and <iOSection>
position 2 using <PDetails>
position 2 data and so on
Expecting OUTPUT is:
<IResponse>
<iOSection>
<Details>
<Info>
<pNumber>FB061689</pNumber>
<indicator>YES</indicator>
<Identifier>1</Identifier>
</Info>
</Details>
<Token>
<Reference>1UUYD05BHM21OJCK3881C7F</Reference>
</Token>
</iOSection>
<iOSection>
<Details>
<Info>
<pNumber>FB061690</pNumber>
<indicator>NO</indicator>
<Identifier>2</Identifier>
</Info>
</Details>
<Token>
<Reference>1UUYD05BHM21OJCK3881C7F</Reference>
</Token>
</iOSection>
</IResponse>
I tried below XSL, but not getting anywhere closer
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all" >
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*[local-name()='IResponse']/*[local-name()='iOSection']/*[local-name()='Details']/*[local-name()='Info']/*[local-name()='indicator']">
<xsl:variable name="RSDetails">
<RS PartID="abcd" SysID="mnc">
<PDetails>
<PN>FB063586</PN>
<PStatus>
<Code>0</Code>
<Description>Cancelled</Description>
</PStatus>
</PDetails>
<PDetails>
<Error>
<Code>92</Code>
<Message>failed</Message>
</Error>
</PDetails>
</RS>
</xsl:variable>
<xsl:element name="indicator">
<xsl:variable name="PStatus">
<xsl:value-of select="$RSDetails/RS/PDetails/PStatus" />
</xsl:variable>
<xsl:variable name="Message">
<xsl:value-of select="$RSDetails/RS/PDetails/Message" />
</xsl:variable>
<xsl:choose>
<xsl:when test="$PStatus='0' and $Message='Cancelled'">
<xsl:value-of select="'YES'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'NO'" />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:copy-of select="@*" />
</xsl:template>
</xsl:stylesheet>
What you want to do here is lookup a value from a matching node in the external file. You didn't say so, but I presume the matching node is the one where the PN
value matches the local pNumber
.
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="RSPDetails" select="document('your_other_file.xml')" />
<xsl:key name="rsp" match="PDetails" use="PN" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="indicator">
<xsl:copy>
<xsl:variable name="pnum" select="../pNumber"/>
<!-- switch context to the other file -->
<xsl:for-each select="$RSPDetails">
<xsl:choose>
<xsl:when test="key('rsp', $pnum)/PStatus/Code=0 and key('rsp', $pnum)/PStatus/Description='Cancelled'">YES</xsl:when>
<xsl:otherwise>NO</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
To base the lookup on position, try:
<xsl:template match="indicator">
<xsl:copy>
<xsl:variable name="i">
<xsl:number count="iOSection"/>
</xsl:variable>
<xsl:variable name="detail" select="$RSPDetails/RS/PDetails[number($i)]"/>
<xsl:choose>
<xsl:when test="$detail/PStatus/Code=0 and $detail/PStatus/Description='Cancelled'">YES</xsl:when>
<xsl:otherwise>NO</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
The <xsl:key>
instruction is not necessary in this scenario.