Iterating through XML tree and finding common node text using XSL

I want to iterate through the tree and find the general text of an element and display it in a table.

<Root>    
 <attr>
    <attrlabl>Attribute Label 1</attrlabl>
    <attrdef>Attribute Definition 1</attrdef>
    <attrdomv>
        <edom>
            <edomv>O</edomv>
            <edomd>Open</edomd>
        </edom>
        <edom>
            <edomv>C</edomv>
            <edomd>Close</edomd>
        </edom>
    </attrdomv>
 </attr>
 <attr>
    <attrlabl>Attribute Label 2</attrlabl>
    <attrdef>Attribute Definition 2</attrdef>
    <attrdomv>
        <edom>
            <edomv>O</edomv>
            <edomd>Open</edomd>
        </edom>
        <edom>
            <edomv>C</edomv>
            <edomd>Close</edomd>
        </edom>
    </attrdomv>
 </attr>
 <attr>
    <attrlabl>Attribute Label 3</attrlabl>
    <attrdef>Attribute Definition 3</attrdef>
    <attrdomv>
        <udom>
            <udomv>No display</udomv>
        </udom>
    </attrdomv>
 </attr>
 <attr>
    <attrlabl>Attribute Label 4</attrlabl>
    <attrdef>Attribute Definition 4</attrdef>
    <attrdomv>
        <edom>
            <edomv>D</edomv>
            <edomd>Different</edomd>
        </edom>
    </attrdomv>
 </attr>
</Root>

      

The result should look something like the one shown in the figure, where only the texts of common elements are displayed. Any help would be much appreciated!

<tr>
<td> For Attribute Label 1 and 2</td>
</tr>
<tr>
<td> Value: O </td>
<td> Description: Open </td>
</tr>
<tr>
<td> Value: C </td>
<td> Description: Close </td>

      

+3


source to share


1 answer


Try something along these lines:

XSLT 2.0

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

<xsl:template match="/Root">
    <table border="1">
        <xsl:for-each-group select="attr/attrdomv/edom" group-by="edomv">
            <xsl:if test="count(current-group()) > 1">
                <tr>
                    <td>
                        <xsl:value-of select="current-group()/ancestor::attr/attrlabl" separator=", "/>
                    </td>
                    <td>
                        <xsl:text>Value: </xsl:text>
                        <xsl:value-of select="current-grouping-key()"/>
                    </td>
                    <td>
                        <xsl:text>Description: </xsl:text>
                        <xsl:value-of select="current-group()[1]/edomd"/>
                    </td>
                </tr>
            </xsl:if>
        </xsl:for-each-group>
    </table>
</xsl:template>

</xsl:stylesheet>

      



The result applied to your example input will be slightly different from yours:

<table border="1">
   <tr>
      <td>Attribute Label 1, Attribute Label 2</td>
      <td>Value: O</td>
      <td>Description: Open</td>
   </tr>
   <tr>
      <td>Attribute Label 1, Attribute Label 2</td>
      <td>Value: C</td>
      <td>Description: Close</td>
   </tr>
</table>

      

mainly because I assumed that ancestral identity was nothing more than a coincidence.

+1


source







All Articles