0

Lets say I have an XML file containing:

<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <location>USA;New York</location>
    <year>1985</year>
  </cd>
  <cd>
    <title>Empire Burlesque II</title>
    <artist>Bob Dylan</artist>
    <location>USA;Houston</location>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <location>UK;London</location>
    <year>1988</year>
  </cd>
.
.
.

My xml file is little bit like this

<bookmark xml-lang="en-US" branch="index" id="bm_id3">
     <bookmark_value>bullet lists;creating while typing</bookmark_value>
     <bookmark_value>lists;automatic numbering</bookmark_value>
.
.
.

Using this xml, I want to generate a list of locations like :

USA
  New York
  Houston
UK
  London

But presently I am only able to get single level list as

USA;New York
USA;Houston
UK;London

How can this be done without doing any change in xml file ?

Regarding code, I am using XSLT 1.0 and the code to get list is (my xml file is different from above mentioned xml file, as it was just to introduce the problem)

<xsl:key name="b_parent" match="bookmark_value" use="substring-before(bookmark_value, ';')" />
<xsl:template match="/">
  <xsl:for-each select="//bookmark[@branch='index']">
    <xsl:for-each select="bookmark_value[count(. | key('b_parent', substring-before(bookmark_value, ';'))[1])=1 ]">
    <xsl:sort select="substring-before(bookmark_value, ';')"/>
    <xsl:variable name="href" select="concat($filename,'#',@id)"/>      
    <li><a href="{$href}" target="_top">
    <xsl:value-of select="substring-before(., ';')"/>
    </a></li>
      <xsl:for-each select="key('b_parent', substring-before(bookmark_value, ';'))">
        <xsl:sort select="substring-after(., ';')" />
        <ul><li><xsl:value-of select="substring-after(., ';')" /></li></ul>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>
Fakabbir Amin
  • 989
  • 7
  • 16
  • 1
    Possible duplicate of [how to apply group by on xslt elements](http://stackoverflow.com/questions/2146648/how-to-apply-group-by-on-xslt-elements) – Joe Mar 05 '17 at 13:44
  • It is very different from that case. Since changes into .xml file is not possible and `` makes it much easier than `USA;Houston`. Also it can have `Germany` which would only be represented by single level. – Fakabbir Amin Mar 05 '17 at 13:52
  • @FakabbirAmin Please post you attempt so that we can fix it, instead of having to write your code for you from scratch. Make sure to clarify whether you're using XSLT 1.0 or 2.0. – michael.hor257k Mar 05 '17 at 14:19
  • @michael.hor257k I have changed the question details to show you my attempt till now. (XSLT 1.0) – Fakabbir Amin Mar 05 '17 at 15:45
  • I see no attempt at grouping in the XSLT you have now added. I suggest you start here: http://www.jenitennison.com/xslt/grouping/muenchian.html and come back if you cannot make it work. – michael.hor257k Mar 05 '17 at 18:32
  • The major part of the problem is that I need to use data from same tag. If it was from two tag,(firstname,lastname) then it would be okay. Perhaps If you could help me with something to extract data from single tag and then use it. Thanks in advance. – Fakabbir Amin Mar 06 '17 at 14:28
  • I already did that. – michael.hor257k Mar 07 '17 at 15:22

1 Answers1

0

Group your records by:

substring-before(concat(location, ';'), ';')
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • This was useful but how to group particularly when more than one file is used to get entries. The above was just helpful for single file. Also sorting has to be considered. – Fakabbir Amin Mar 15 '17 at 12:16
  • @FakabbirAmin There is only one file in your question. If you have an additional problem handling more than one file, I suggest you post a new question and show more than one file (along with your current solution and the expected output) there. I don't see what the problem with sorting is - but then I don't know what you want to sort by (the expected output in your question does no seem ton be sorted at all). – michael.hor257k Mar 15 '17 at 14:26