1

I'm looking for XLST to edit an xml file. As explained in the title, I would like to rename my items with the name of their "name" attribute.

My xml file :

<doe name="titi">
    <bob name="toto">blabla</bob>
    <bob name="tutu">blabla </bob>
</doe>

The output xml file that I would like:

<titi>
    <toto>blabla</toto>
    <tutu>blabla </tutu>
</titi>

I tried several things but I was sent back an "attribute" name "hab a bad value"

<xsl:element name="@bob">
    <xsl:value-of select="$bob" />  
</xsl:element>

or

<xsl:element name="/doe/@bob">
    <xsl:value-of select="$bob" />  
</xsl:element>

2 Answers 2

1

Following code as your requirment

<xsl:template match="doe">
<xsl:element name="{@name}">
<xsl:for-each select="bob">
    <xsl:element name="{@name}"><xsl:value-of select="."/></xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

<xsl:template match="*[@name]">
        <xsl:element name="{@name}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

Check transformation at https://xsltfiddle.liberty-development.net/jyRYYj7

1 Comment

Of course, it will only work if the value of the attribute is a valid XML element name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.