xmlxsltvalue-of

xsl value of as xsl element name


I have the following structure

<Rowsets>
<Rowset>
    <Row>
        <ID>123</ID>
        <PropertyID>property 1</PropertyID>
        <PropertyValue>value 1</PropertyValue>
    </Row>
    <Row>
        <ID>123</ID>
        <PropertyID>property 2</PropertyID>
        <PropertyValue>value 2</PropertyValue>
    </Row>
    <Row>
        <ID>456</ID>
        <PropertyID>property 1</PropertyID>
        <PropertyValue>value 11</PropertyValue>
    </Row>
    <Row>
        <ID>456</ID>
        <PropertyID>property 2</PropertyID>
        <PropertyValue>value 22</PropertyValue>
    </Row>
</Rowset>

I want to group the properties together with the ID, as in the following structure

<SEGMENTS>
<SET>
    <ID>123</ID>
    <property 1>value 1</property 1>
    <property 2>value 2</property 2>
</SET>
<SET>
    <ID>456</ID>
    <property 1>value 11</property 1>
    <property 2>value 22</property 2>
</SET>

I do this with this XSLT form

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="ID-sets" match="Row" use="ID" />
<xsl:template match="Rowsets/Rowset">
    <SEGMENTS>
        <xsl:for-each select="Row[count(. | key('ID-sets', ID)[1]) = 1]">
            <xsl:sort select="ID" />
            <SET>
                <ID>
                    <xsl:value-of select="ID" />
                </ID>

                <xsl:for-each select="key('ID-sets', ID)">
                    <xsl:sort select="PropertyID" />
                    <xsl:element name="PropertyID">
                        <xsl:value-of select="PropertyValue"/>
                    </xsl:element>
                </xsl:for-each>
            </SET>
        </xsl:for-each>
    </SEGMENTS>
</xsl:template>

I'm almost there, but the one thing that won't work is the element name. After some reseach I found that I need to use this: xsl:element name="{PropertyID}"

Except, it doesn't work. Notepad++ gives the warning that it's unable to apply transformation on current source. Without the {} tags it works, but then it's just static and not the variable PropertyID.

It's probably a little thing, but I can't find it. I'm limited to xslt 1.0 if that matters.


Solution

  • Spaces are not allowed in element names. An alternative is

    <Property id="{PropertyID}">
    

    Thanks to Tim C for the answer.