1

I have an XML input with elements that have a number of attributes. I don't know in advance, which attributes.

I'd like to create an attribute element of the same name for each of the existing attributes.

Input:

<elem id="1" name="test" version="2" />
<elem id="2" check="true" base="dir"/>

output:

<newelem newattribute="bla" newattribute2="blabla" id="1" name="test" version="2"/>
<newelem newattribute="bla" newattribute2="blabla" id="2" check="true" base="dir"/>

I tried this:

    <xsl:for-each select="@*">
        <xsl:attribute name="name(.)" select="."/>
    </xsl:for-each>

But the name() function doesn't work here, it seems.

What's the right way to do this?

3 Answers 3

3

There's a simpler way, just use <xsl:copy-of select="@*"/>

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

1 Comment

aaah, right, I always forget about those copy-elements. Thank you very much!
3

put name function between {}

 <xsl:for-each select="@*">
        <xsl:attribute name="{name(.)}" select="."/>
 </xsl:for-each>

I hope this could help.

Comments

1

You can surround the call to name(.) with curly braces:

<xsl:attribute name="{name(.)}" select="."/>

Comments

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.