0

I have an XML file of following structure

    <root>
      <Row>
         <KeyName>A</KeyName>
         .....
      </Row>
      <Row>
         <KeyName>B</KeyName>
         .....
      </Row>
      <Row>
         <KeyName>A</KeyName>
         .....
      </Row>
      <Row>
         <KeyName>B</KeyName>
         .....
      </Row>
      <Row>
         <KeyName>C</KeyName>
         .....
      </Row>
   </root>

I would like to make an statement for a xml-fo transformation

I need to make a loop over all KeyNames, but each KeyName only once. My Problem is, that I don't know which KeyNames will be used and how often they appear. The structure/depth of the tree is constant.

Goal:

    Block KeyName/text()=A
     Row data
     Row data
     Row data 
     ..

    Block KeyName/text()=B
     Row data
     Row data
     ..

Continued for all existing (but unknown) KeyNames.

Lars
  • 11
  • 1
  • I don't know xml-fo, but [this old question](https://stackoverflow.com/q/38197728/243245) sounds similar to your requirements? – Rup May 03 '18 at 17:27

1 Answers1

0

maybe it helps some other people. After some testing and more research, i found a routine called "Muenchian grouping". I tried different examples until I found a suitable one. Following you will find the code for the xslt transform file, which refers to the data of my question.

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

<xsl:key name="RowbyKeyName" match="Row" use="KeyName" />

<xsl:template match="root"> 
   <ul>
     <xsl:for-each select="Row[generate-id() = generate-id(key('RowbyKeyName', KeyName)[1])]" >
        <li> 
             KeyName: <xsl:value-of select="KeyName" /> 
             <ul> 
                      <xsl:for-each select="key('RowbyKeyName', KeyName)" >
                         <li>  
                             <xsl:value-of select="..." /> 
                             ....
                             Row Data
                             ....
                         </li>
                      </xsl:for-each> 
             </ul>
       </li> 
     </xsl:for-each> 
   </ul> 
</xsl:template>
</xsl:stylesheet>
Lars
  • 11
  • 1