Reputation: 7
I am trying to use xslt in one of my product and i am stuck up on below issue.
As a response I am getting an XML which has some fields, More likely there are two main List Node Header
and Details
, Details
is main data return by processing and Header
has coded and other data related to output, In case error code
is 70
, which means, this is error, other error codes are treated as successful.
XML Response Example:
<?xml version="1.0"?>
<root>
<subroot>
<data>
<Header>
<SCode>1</SCode>
<StatusCode>0</StatusCode>
<ErrorCode>00000000</ErrorCode>
<ErrorDesc>This is OK</ErrorDesc>
</Header>
<Details>
<accountNumber>1111X</accountNumber>
<accountType>D</accountType>
<Total>10000</Total>
<Interest>10</Interest>
<NewFlag>N</NewFlag>
</Details>
</data>
<data>
<Header>
<SCode>1</SCode>
<StatusCode>12</StatusCode>
<ErrorCode>70</ErrorCode>
<ErrorDesc>This is ERROR</ErrorDesc>
</Header>
<Details>
<accountNumber>2222</accountNumber>
<accountType>D</accountType>
<Total>1000</Total>
<Interest>12</Interest>
<NewFlag>Y</NewFlag>
</Details>
</data>
</subroot>
</root>
Now Output should be
<?xml version="1.0"?>
<root>
<subroot>
<data>
<Details>
<accountNumber>1111X</accountNumber>
<accountType>D</accountType>
<Total>10000</Total>
<Interest>10</Interest>
<NewFlag>N</NewFlag>
</Details>
</data>
<data>
<Details>
<ErrorCode>70</ErrorCode>
<ErrorDesc>This is ERROR</ErrorDesc>
<accountNumber>2222</accountNumber>
<accountType>D</accountType>
</Details>
</data>
</subroot>
</root>
Thanks!!
Upvotes: 0
Views: 63
Reputation: 3247
Based on the sample input XML and the desired output shared, you can do the following to achieve the results.
Start with an identity transform template that will copy the structure as-is to the output.
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
Using <xsl:for-each>
on the <Header>
node and using the node's position()
as a reference, appropriate account information can be captured in the output. Below is the XSLT for reference with the output.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="data">
<xsl:copy>
<xsl:for-each select="Header">
<xsl:variable name="pos" select="position()" />
<xsl:choose>
<xsl:when test="ErrorCode = '70'">
<Details>
<xsl:apply-templates select="ErrorCode" />
<xsl:apply-templates select="ErrorDesc" />
<xsl:apply-templates select="../Details[$pos]/accountNumber" />
<xsl:apply-templates select="../Details[$pos]/accountType" />
</Details>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="../Details[$pos]" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output
<root>
<subroot>
<data>
<Details>
<accountNumber>1111X</accountNumber>
<accountType>D</accountType>
<Total>10000</Total>
<Interest>10</Interest>
<NewFlag>N</NewFlag>
</Details>
</data>
<data>
<Details>
<ErrorCode>70</ErrorCode>
<ErrorDesc>This is ERROR</ErrorDesc>
<accountNumber>2222</accountNumber>
<accountType>D</accountType>
</Details>
</data>
</subroot>
</root>
Upvotes: 1