Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping XML nodes by attribute value in XSLT

Tags:

xml

xslt

xpath

Im quite new to xslt transforms and I need help with one kind of transformation. I need to group all nodes of certain type by one of it atributes and list parents of every kind of attribute. It is a kind of making summary of usage of certain things in the document. The I will present simplified example.
Input:

<root>
<node name="node1">
    <somechild child-id="1">
</node>
<node name="node2">
    <somechild child-id="2">
</node>
<node name="node3">
    <somechild child-id="1">
</node>
<node name="node4">
    <somechild child-id="2">
</node>
<node name="node5">
    <somechild child-id="3">
</node>
</root>

Desired output:

<root>
<somechild child-id="1">
    <is-child-of>
        <node name="node1" />
        <node name="node3" />
    </is-child-of>
</somechild>
<somechild child-id="2">
    <is-child-of>
        <node name="node2" />
        <node name="node4" />
    </is-child-of>
</somechild>
<somechild child-id="3">
    <is-child-of>
        <node name="node5" />
    </is-child-of>
</somechild>
</root>

Idea is that if is the same element in many nodes they have same child-id. I need to find all used by every . I found this question XSLT transformation to xml, grouping by key which is kind of similar but there is a declaration of all authors on the begining and I don't have such, is allways only a child of .

like image 861
Pax0r Avatar asked Aug 30 '11 13:08

Pax0r


People also ask

What is current group () in XSLT?

Returns the contents of the current group selected by xsl:for-each-group. Available in XSLT 2.0 and later versions. Available in all Saxon editions. current-group() ➔ item()*

What is Number () in XSLT?

Specifies the format pattern. Here are some of the characters used in the formatting pattern: 0 (Digit)


2 Answers

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

    <xsl:key name="k" match="somechild" use="@child-id"/>
    <xsl:key name="n" match="node" use="somechild/@child-id"/>

    <xsl:template match="root">
        <xsl:copy>
            <xsl:apply-templates 
                select="//somechild[generate-id(.) = generate-id(key('k', @child-id))]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="somechild">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>

            <is-child-of>
                <xsl:apply-templates select="key('n', @child-id)"/>
            </is-child-of>
        </xsl:copy>

    </xsl:template>

    <xsl:template match="node">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Output:

<root>
  <somechild child-id="1">
    <is-child-of>
      <node name="node1" />
      <node name="node3" />
    </is-child-of>
  </somechild>
  <somechild child-id="2">
    <is-child-of>
      <node name="node2" />
      <node name="node4" />
    </is-child-of>
  </somechild>
  <somechild child-id="3">
    <is-child-of>
      <node name="node5" />
    </is-child-of>
  </somechild>
</root>
like image 174
Kirill Polishchuk Avatar answered Sep 30 '22 17:09

Kirill Polishchuk


You could try the following approach?

<!-- select the current child id to filter by -->
<xsl:variable name="id" select="somechild/@child-id"/>
<!-- select the nodes which have a somechild element with the child-id to look for -->
<xsl:for-each select="/root//some-child[@child-id = $id]/..">
   <!-- for each such node, do something -->
</xsl:for-each>
like image 33
user268396 Avatar answered Sep 30 '22 18:09

user268396