Ramki
Ramki

Reputation: 381

Converting a data base output to xml

Sorry guys I have updated the Issue below.

Though looked to be really simple earlier, later turned into a really complex thing. Can anyone help me out?

I need this XML

<?xml version="1.0" encoding="UTF-8"?>


<RecordSet>
    <Data image="h1.gif" description="a"/>
    <Data image="" description="asdf" />
    <Data image="" description="bsdf"/>
    <Data image="" description="csdf"/>
    <Data image="h2.gif" description="b"/>
    <Data image="" description="dsdf"/>
    <Data image="" description="esdf"/>
    <Data image="h3.gif" description="c"/>
    <Data image="" description="sdff"/>
</RecordSet>

converting to this

 <RecordSet>
    <MenuHeader image="h1.gif">
        <Menu description="a"/>
        <Menu description="b"/>
        <Menu description="c"/>
    </MenuHeader>
    <MenuHeader image="h2.gif">
        <Menu description="d"/>
        <Menu description="e"/>
    </MenuHeader>
    <MenuHeader image="h3.gif">
        <Menu description="f"/>
    </MenuHeader>
</RecordSet>

Stylesheet used

<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:template match="RecordSet">
 <RecordSet>
  <xsl:for-each select="Data[not(@image='')]">
   <MenuHeader image="{@image}">
    <xsl:copy-of select="key('d',@image)"/>
   </MenuHeader>
  </xsl:for-each>
 </RecordSet>
</xsl:template>

<xsl:key name="d"
     match="Data[@description]" 
     use="preceding-sibling::Data[@image][1]/@image"/>

</xsl:stylesheet>

Actual output:

<?xml version="1.0" encoding="UTF-8"?>
<RecordSet>
    <MenuHeader image="h1.gif">
        <Data image="" description="asdf"/>
    </MenuHeader>
    <MenuHeader image="h2.gif">
        <Data image="" description="dsdf"/>
    </MenuHeader>
    <MenuHeader image="h3.gif">
        <Data image="" description="sdff"/>
    </MenuHeader>
</RecordSet>

Updated: The Data node that does have the image attribute with value should be set as MenuHeader. Any help will be of really great help.

Upvotes: 1

Views: 131

Answers (4)

ABach
ABach

Reputation: 3738

The accepted answer does not produce the output you say you want in your original post. This slightly modified version (still XSLT 1.0) should do the trick.

Here is a different XSLT 1.0 alternative.

When this XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key
     name="kDataByPrecedingImage"
     match="Data[@image = '']"
     use="generate-id(preceding-sibling::*[@image != ''][1])"/>

  <xsl:template match="RecordSet">
    <RecordSet>
      <xsl:apply-templates select="*[@image != '']"/>
    </RecordSet>
  </xsl:template>

  <xsl:template match="Data">
    <MenuHeader image="{@image}">
      <xsl:apply-templates
        select="key('kDataByPrecedingImage', generate-id())"
        mode="children"/>
    </MenuHeader>
  </xsl:template>

  <xsl:template match="Data" mode="children">
    <Menu description="{@description}"/>
  </xsl:template>

</xsl:stylesheet>

...is applied against the given XML:

<?xml version="1.0" encoding="UTF-8"?>
<RecordSet>
  <Data image="h1.gif" description="a"/>
  <Data image="" description="asdf"/>
  <Data image="" description="bsdf"/>
  <Data image="" description="csdf"/>
  <Data image="h2.gif" description="b"/>
  <Data image="" description="dsdf"/>
  <Data image="" description="esdf"/>
  <Data image="h3.gif" description="c"/>
  <Data image="" description="sdff"/>
</RecordSet>

...the desired result is produced:

<?xml version="1.0" encoding="UTF-8"?>
<RecordSet>
  <MenuHeader image="h1.gif">
    <Menu description="asdf"/>
    <Menu description="bsdf"/>
    <Menu description="csdf"/>
  </MenuHeader>
  <MenuHeader image="h2.gif">
    <Menu description="dsdf"/>
    <Menu description="esdf"/>
  </MenuHeader>
  <MenuHeader image="h3.gif">
    <Menu description="sdff"/>
  </MenuHeader>
</RecordSet>

Upvotes: 1

David Carlisle
David Carlisle

Reputation: 5652

This is easier in XSLT 2, but using XSLT 1 for old time's sake:

<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:template match="RecordSet">
 <RecordSet>
  <xsl:for-each select="Data[string(@image)]">
   <MenuHeader image="{@image}">
    <xsl:for-each select="key('d',@image)">
     <Menu>
      <xsl:copy-of select="@description"/>
     </Menu>
    </xsl:for-each>
   </MenuHeader>
  </xsl:for-each>
 </RecordSet>
</xsl:template>

<xsl:key name="d"
     match="Data[not(string(@image))][@description]" 
     use="preceding-sibling::Data[string(@image)][1]/@image"/>

</xsl:stylesheet>

Produces

<?xml version="1.0" encoding="utf-8"?>
<RecordSet>
   <MenuHeader image="h1.gif">
      <Menu description="asdf"/>
      <Menu description="bsdf"/>
      <Menu description="csdf"/>
   </MenuHeader>
   <MenuHeader image="h2.gif">
      <Menu description="dsdf"/>
      <Menu description="esdf"/>
   </MenuHeader>
   <MenuHeader image="h3.gif">
      <Menu description="sdff"/>
   </MenuHeader>
</RecordSet>

Note for the updated version of the question where you want have image="" rather than no image attribute, the test is string(@image) rather than simply @image.

Upvotes: 1

Ian Roberts
Ian Roberts

Reputation: 122374

Here's an alternative approach using tail-recursion to approximate a "while" loop

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:template match="/RecordSet">
    <RecordSet>
      <xsl:apply-templates select="Data[@image != '']"/>
    </RecordSet>
  </xsl:template>

  <xsl:template match="Data[@image != '']">
    <MenuHeader image="{@image}">
      <xsl:apply-templates select="following-sibling::Data[1][@image='']" />
    </MenuHeader>
  </xsl:template>

  <xsl:template match="Data">
    <Menu description="{@description}"/>
    <xsl:apply-templates select="following-sibling::Data[1][@image='']" />
  </xsl:template>
</xsl:stylesheet>

The trick here is that the template generating the MenuHeader applies templates to just the first (if any) non-image Data element that follows it. That template then recursively consumes the next one etc. until we reach a point where the next one has an image, when the recursion will automatically stop.

Upvotes: 1

Daniel Haley
Daniel Haley

Reputation: 52858

2.0 option, push style.

XML Input

<RecordSet>
    <Data image="h1.gif"/>
    <Data description="a"/>
    <Data description="b"/>
    <Data description="c"/>
    <Data image="h2.gif"/>
    <Data description="d"/>
    <Data description="e"/>
    <Data image="h3.gif"/>
    <Data description="f"/>
</RecordSet>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each-group select="Data" group-starting-with="Data[@image]">
                <MenuHeader image="{@image}">
                    <xsl:apply-templates select="current-group()[not(@image)]"/>
                </MenuHeader>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Data">
        <Menu>
            <xsl:apply-templates select="@*|node()"/>
        </Menu>
    </xsl:template>

</xsl:stylesheet>

Output

<RecordSet>
   <MenuHeader image="h1.gif">
      <Menu description="a"/>
      <Menu description="b"/>
      <Menu description="c"/>
   </MenuHeader>
   <MenuHeader image="h2.gif">
      <Menu description="d"/>
      <Menu description="e"/>
   </MenuHeader>
   <MenuHeader image="h3.gif">
      <Menu description="f"/>
   </MenuHeader>
</RecordSet>

Upvotes: 1

Related Questions