0

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.

Pieter_mov
  • 15
  • 7
  • You are not allowed spaces in element names, so a name of `Property 1` is not valid in XML. You would have to strip out the space, to give `` for example, or replace the space with a symbol, like an underscore. – Tim C Jul 06 '17 at 12:54
  • I already thought about that, so there is no other way then just avoiding spaces? I'll just go with two static tags then, for the property name and the value. Ugly, but a lot less work than renaming everything. Thanks! – Pieter_mov Jul 06 '17 at 13:02
  • You could do something like `` perhaps? – Tim C Jul 06 '17 at 13:19
  • That seems the closest I can get to a nice XML structure. Thanks! – Pieter_mov Jul 06 '17 at 13:26

1 Answers1

0

Spaces are not allowed in element names. An alternative is

<Property id="{PropertyID}">

Thanks to Tim C for the answer.

Pieter_mov
  • 15
  • 7