Having trouble fromatting the xml. Need the below xml to be formatted to the mentioned format. need the values to be transalated to lower case too.
Input XML
`<?xml version="1.0" encoding="UTF-8"?>
<serviceConfig>
<consumer id="1">
<cn>A</cn>
<cn>B</cn>
<cn>C</cn>
</consumer>
<consumer id="2">
<cn>D</cn>
<cn>E</cn>
</consumer>
<consumer id="3">
<cn>F</cn>
</consumer>
<consumer id="4">
<cn>G</cn>
<cn>H</cn>
<cn>I</cn>
</consumer>
<consumer id="5">
<cn>J</cn>
<cn>K</cn>
<cn>M</cn>
</consumer>
</serviceConfig>`
Required output
<serviceConfig>
<consumer id="1">a</consumer>
<consumer id="1">b</consumer>
<consumer id="1">c</consumer>
<consumer id="2">d</consumer>
<consumer id="2">e</consumer>
<consumer id="3">f</consumer>
<consumer id="4">g</consumer>
<consumer id="4">h</consumer>
<consumer id="4">i</consumer>
<consumer id="5">j</consumer>
<consumer id="5">k</consumer>
<consumer id="5">m</consumer>
</serviceConfig>
using xsl version 1.0
When i am using below xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<serviceConfig>
<xsl:for-each select="//consumer/cn">
<consumer>
<xsl:attribute name="id"><xsl:value-of select = "//consumer/@id"/></xsl:attribute>
<xsl:value-of select = "."/>
</consumer>
</xsl:for-each>
</serviceConfig>
</xsl:template>
</xsl:stylesheet>
all my id are being returned from the first occurrence.but need the corresponding id for that element
<serviceConfig>
<consumer id="1">A</consumer>
<consumer id="1">B</consumer>
<consumer id="1">C</consumer>
<consumer id="1">D</consumer>
<consumer id="1">E</consumer>
<consumer id="1">F</consumer>
<consumer id="1">G</consumer>
<consumer id="1">H</consumer>
<consumer id="1">I</consumer>
<consumer id="1">J</consumer>
<consumer id="1">K</consumer>
<consumer id="1">M</consumer>
</serviceConfig>
CodePudding user response:
Please try the following XSLT.
It is using a so called Identity Transform pattern.
Input XML
<?xml version="1.0" encoding="UTF-8"?>
<serviceConfig>
<consumer id="1">
<cn>A</cn>
<cn>B</cn>
<cn>C</cn>
</consumer>
<consumer id="2">
<cn>D</cn>
<cn>E</cn>
</consumer>
<consumer id="3">
<cn>F</cn>
</consumer>
<consumer id="4">
<cn>G</cn>
<cn>H</cn>
<cn>I</cn>
</consumer>
<consumer id="5">
<cn>J</cn>
<cn>K</cn>
<cn>M</cn>
</consumer>
</serviceConfig>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="consumer">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="cn">
<consumer id="{../@id}">
<xsl:value-of select="."/>
</consumer>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version='1.0' encoding='UTF-8' ?>
<serviceConfig>
<consumer id="1">A</consumer>
<consumer id="1">B</consumer>
<consumer id="1">C</consumer>
<consumer id="2">D</consumer>
<consumer id="2">E</consumer>
<consumer id="3">F</consumer>
<consumer id="4">G</consumer>
<consumer id="4">H</consumer>
<consumer id="4">I</consumer>
<consumer id="5">J</consumer>
<consumer id="5">K</consumer>
<consumer id="5">M</consumer>
</serviceConfig>
CodePudding user response:
Your stylesheet will work as expected, if you change:
<xsl:value-of select = "//consumer/@id"/><
to:
<xsl:value-of select = "../@id"/>
See: https://www.w3.org/TR/1999/REC-xpath-19991116/#path-abbrev