xml - XSLT to copy value in a node to another across repeating nodes -
very new xslt , needs in transforming xml. below xml can have multiple "row" tags
<?xml version="1.0" encoding="utf-8"?> <ns1:row> <ns1:city>baltimore</ns1:city> <ns1:miscdata> <ns1:building> <ns1:vendorcode>123</ns1:vendorcode> <ns1:value>2</ns1:value> </ns1:building> <ns1:building> <ns1:vendorcode>345</ns1:vendorcode> <ns1:value>8</ns1:value> </ns1:building> </ns1:miscdata> </ns1:row> <ns1:row> <ns1:city>fremont</ns1:city> <ns1:miscdata> <ns1:building> <ns1:vendorcode>332</ns1:vendorcode> <ns1:value>4</ns1:value> </ns1:building> <ns1:building> <ns1:vendorcode>342</ns1:vendorcode> <ns1:value>14</ns1:value> </ns1:building> <ns1:building> <ns1:vendorcode>323</ns1:vendorcode> <ns1:value>233</ns1:value> </ns1:building> </ns1:miscdata> </ns1:row>
the value in "vendorcode" tag in above xml needs copied "value" tag. output xml
<?xml version="1.0" encoding="utf-8"?> <ns1:row> <ns1:city>baltimore</ns1:city> <ns1:miscdata> <ns1:building> <ns1:vendorcode>123</ns1:vendorcode> <ns1:value>123</ns1:value> </ns1:building> <ns1:building> <ns1:vendorcode>345</ns1:vendorcode> <ns1:value>345</ns1:value> </ns1:building> </ns1:miscdata> </ns1:row> <ns1:row> <ns1:city>fremont</ns1:city> <ns1:miscdata> <ns1:building> <ns1:vendorcode>332</ns1:vendorcode> <ns1:value>332</ns1:value> </ns1:building> <ns1:building> <ns1:vendorcode>342</ns1:vendorcode> <ns1:value>342</ns1:value> </ns1:building> <ns1:building> <ns1:vendorcode>323</ns1:vendorcode> <ns1:value>323</ns1:value> </ns1:building> </ns1:miscdata> </ns1:row>
all such tasks use template
<xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template>
as starting point, add template nodes need special treatment e.g. in case
<xsl:template match="ns1:building/ns1:value"> <xsl:copy> <xsl:value-of select="preceding-sibling::ns1:vendorcode"/> </xsl:copy> </xsl:template>
Comments
Post a Comment