coder
coder

Reputation: 4283

XSLT elements to attributes in a new element

I'm able to convert from elements to attributes and put them in a new element, but the parent element is repeating for each child elements.

Xml:

<root>
    <function>test</function>
    <transaction>
        <date>2019-09-24</date>
        <user>myuser</user>
        <items>
            <number>4884624</number>
            <unit>EA</unit>
            <qty>6</qty>
            <barcode/>
            <conversion/>
        </items>
        <items>
            <number>4895036</number>
            <unit>CS</unit>
            <qty>4</qty>
            <barcode/>
            <conversion/>
        </items>
        <items>
            <number/>
            <unit>CS</unit>
            <qty>4</qty>
            <barcode/>
            <conversion/>
        </items>
    </transaction>
</root>

Xslt:

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="/ | node() | @* | comment() | processing-instruction()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="items">
        <xsl:copy>
            <item>
                <xsl:for-each select="*">
                    <xsl:attribute name="{local-name()}">
                        <xsl:value-of select="."/>
                    </xsl:attribute>
                </xsl:for-each>
            </item>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

Current output:

<root>
    <function>test</function>
    <transaction>
        <date>2019-09-24</date>
        <user>myuser</user>
        <items>
            <item number="4884624" unit="EA" qty="6" barcode="" conversion=""/>
        </items>
        <items>
            <item number="4895036" unit="CS" qty="4" barcode="" conversion=""/>
        </items>
        <items>
            <item number="" unit="CS" qty="4" barcode="" conversion=""/>
        </items>
    </transaction>
</root>

Desired output:

<root>
    <function>test</function>
    <transaction>
        <date>2019-09-24</date>
        <user>myuser</user>
        <items>
            <item number="4884624" unit="EA" qty="6" barcode="" conversion=""/>
            <item number="4895036" unit="CS" qty="4" barcode="" conversion=""/>
            <item number="" unit="CS" qty="4" barcode="" conversion=""/>
        </items>
    </transaction>
</root>

Upvotes: 1

Views: 61

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116992

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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="transaction">
    <xsl:copy>
        <xsl:apply-templates select="*[not(self::items)]"/>
        <items>
            <xsl:apply-templates select="items"/>
        </items>
    </xsl:copy>
</xsl:template>

<xsl:template match="items">
    <item>
        <xsl:apply-templates/>
    </item>
</xsl:template>

<xsl:template match="items/*">
    <xsl:attribute name="{local-name()}">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

Upvotes: 2

Related Questions