0

I am searching for a solution to transform this XML :

<A>
    <B>
        <X id="aaa" text="xyz" unit="mm">asdf</X>
        <X id="bbb" text="xyz" unit="mm">asdf</X>
        <X id="ccc" text="xyz" unit="mm">asdf</X>
        ...
    </B>
    <C>
        <Y id="aaa" text="xyz" unit="mm">asdf</Y>
        <Y id="bbb" text="xyz" unit="mm">asdf</Y>
        <Y id="ccc" text="xyz" unit="mm">asdf</Y>
        ...
    </C>
    <D>
        <Z id="aaa" text="xyz" unit="mm">asdf</Z>
        <Z id="bbb" text="xyz" unit="mm">asdf</Z>
        <Z id="ccc" text="xyz" unit="mm">asdf</Z>
        ...
    </D>
    ...
</A>

into this:

<A>
    <B_X_aaa id="aaa" text="xyz" unit="mm">asdf</B_X_aaa>
    <B_X_bbb id="bbb" text="xyz" unit="mm">asdf</B_X_bbb>
    <B_X_ccc id="ccc" text="xyz" unit="mm">asdf</B_X_ccc>
    <C_Y_aaa id="aaa" text="xyz" unit="mm">asdf</C_Y_aaa>
    <C_Y_bbb id="bbb" text="xyz" unit="mm">asdf</C_Y_bbb>
    <C_Y_ccc id="ccc" text="xyz" unit="mm">asdf</C_Y_ccc>
    <D_Z_aaa id="aaa" text="xyz" unit="mm">asdf</D_Z_aaa>
    <D_Z_bbb id="bbb" text="xyz" unit="mm">asdf</D_Z_bbb>
    <D_Z_ccc id="ccc" text="xyz" unit="mm">asdf</D_Z_ccc>
</A>

So the last part of the transformed xml node name has to be the 'id' attribute value and the attributes have to be copied all

Thank you

1 Answer 1

1

Try:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/A">
    <xsl:copy>
        <xsl:for-each select="*/*">
            <xsl:element name="{name(..)}_{name()}_{@id}">
                <xsl:copy-of select="@* | node()"/>
            </xsl:element>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

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.