Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert XML element names using the element's attribute value with XSLT?

I am working on an XML dataset, however I am not quite sure how to convert it to my desired format using XSLT so I can work with it. Currently I have an XML with element’s that aren’t useful to me and name attributes which hold the names of what the elements should actually be named after. So currently an element is called <L7j:col> whilst I’d want it to be named after the ‘name’ attribute, for example <firstname>.
Could anyone please help me with this?

Current (example) XML:

<?xml version="1.0" encoding="UTF-8"?>
<L7j:jdbcQueryResult xmlns:L7j="http://ns.l7tech.com/2012/08/jdbc-query-result">
    <L7j:row>
        <L7j:col name="firstname">Stefan</L7j:col>
        <L7j:col name="lastname">McLovin</L7j:col>
        <L7j:col name="age" type="java.lang.String">21</L7j:col>
        <L7j:col name="year" type="java.math.BigDecimal">3</L7j:col>
        <L7j:col name="id">0001</L7j:col>
    </L7j:row>
    <L7j:row>
        <L7j:col name="firstname">Kevin</L7j:col>
        <L7j:col name="lastname">McCool</L7j:col>
        <L7j:col name="age" type="java.lang.String">25</L7j:col>
        <L7j:col name="year" type="java.math.BigDecimal">4</L7j:col>
        <L7j:col name="id">0002</L7j:col>
    </L7j:row>
</L7j:jdbcQueryResult>

The XSLT I found to convert the XML:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:L7j="https://ns.l7tech.com/2012/08/jdbc-query-result">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="*">
        <xsl:element name="{name()}">
            <xsl:for-each select="@*">
                <xsl:element name="{name()}">
                    <xsl:value-of select="."/>
                </xsl:element>
            </xsl:for-each>
            <xsl:apply-templates select="*|text()"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Source

Current Output:

<?xml version="1.0" encoding="UTF-8"?>
<L7j:jdbcQueryResult xmlns:L7j="https://ns.l7tech.com/2012/08/jdbc-query-result">
    <L7j:row>
        <L7j:col>
            <name>firstname</name>Stefan</L7j:col>
        <L7j:col>
            <name>lastname</name>Meeuwessen</L7j:col>
        <L7j:col>
            <name>age</name>
            <type>java.lang.String</type>21</L7j:col>
        <L7j:col>
            <name>year</name>
            <type>java.math.BigDecimal</type>3</L7j:col>
        <L7j:col>
            <name>id</name>0001</L7j:col>
    </L7j:row>
    <L7j:row>
        <L7j:col>
            <name>firstname</name>Kevin</L7j:col>
        <L7j:col>
            <name>lastname</name>Meeuwessen</L7j:col>
        <L7j:col>
            <name>age</name>
            <type>java.lang.String</type>25</L7j:col>
        <L7j:col>
            <name>year</name>
            <type>java.math.BigDecimal</type>4</L7j:col>
        <L7j:col>
            <name>id</name>0002</L7j:col>
    </L7j:row>
</L7j:jdbcQueryResult>

Desired output:

<?xml version="1.0" encoding="UTF-8"?>
<L7j:jdbcQueryResult xmlns:L7j="http://ns.l7tech.com/2012/08/jdbc-query-result">
    <L7j:row>
        <firstname>Stefan</firstname>
        <lastname>Meeuwessen</lastname>
        <age>21</age>
        <year>3</year>
        <id>0001</id>
    </L7j:row>
    <L7j:row>
        <firstname>Kevin</firstname>
        <lastname>Meeuwessen</lastname>
        <age>25</age>
        <year>4<year>
        <id>0002</id>
    </L7j:row>
</L7j:jdbcQueryResult>

Thanks for any help!

>Solution :

The output you want can be produced by:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:L7j="http://ns.l7tech.com/2012/08/jdbc-query-result">
    <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="L7j:col">
        <xsl:element name="{@name}">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Pay attention to the xmlns:L7j namespace declaration. In your input (and in my stylesheet) it is:

xmlns:L7j="http://ns.l7tech.com/2012/08/jdbc-query-result"

but in your stylesheet it is:

xmlns:L7j="https://ns.l7tech.com/2012/08/jdbc-query-result"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading