Just tryng to learn XSLT. Please refer Input and Output documents Input xml is only a potion of the document. There are more record levels in this document
<?xml version="1.0" encoding="UTF-8"?>
<record level="1">
<GROUP>ABC</GROUP>
<NAME>1100000000111</NAME>
</record>
Desired output
I would like to rename <record level="1">
to <record_level1>
<?xml version="1.0" encoding="UTF-8"?>
<record_level1>
<GROUP>ABC</GROUP>
<NAME>1100000000111</NAME>
</record_level1>
CodePudding user response:
If you start with an identity transform to copy all content as the default behavior, then you can add a specialized template to match the record
elements that have a @level
attribute. Inside that template, create an element with the name concatenating the element name with the attribute name and value:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="record[@level]">
<xsl:element name="{concat(name(), '_level', @level)}">
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Note the {}
curly braces used inside of the element name attribute. It would otherwise expect a string literal. The attribute value template allows you to execute code to construct that name value, using the concat()
function.