XSL formatting help..

Discussion in 'XML' started by icedgar, Mar 2, 2005.

  1. icedgar

    icedgar Guest

    I currently have the following XML:

    <AAA>
    <dict>
    <a>apple</a>
    <dict>
    <a>2</a>
    <dict>
    <a>James</a>
    <x>1</x>
    <a>Tammy</a>
    <y>2</y>
    <a>Pat</a>
    <z>3</z>
    <a>George</a>
    <z>4</z>
    <a>Fred</a>
    <x>5</x>
    </dict>
    <a>4</a>
    <dict>
    <a>James</a>
    <x>1</x>
    <a>Tammy</a>
    <y>2</y>
    <a>Pat</a>
    <z>3</z>
    <a>George</a>
    <z>4</z>
    <a>Fred</a>
    <z>5</z>
    </dict>
    </dict>
    </dict>
    </AAA>

    When I apply the following XSL template:

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


    <xsl:template match="/AAA/dict/dict/*">
    <xsl:copy>
    <xsl:apply-templates select="a"/>
    </xsl:copy>
    </xsl:template>


    <xsl:template match="a">
    <xsl:attribute name="{translate(.,' ','_')}">
    <xsl:apply-templates select="following-sibling::*[1]"/>

    </xsl:attribute>
    </xsl:template>
    </xsl:stylesheet>

    I get the following results:
    <a></a>
    <dict James="1" Tammy="2" Pat="3" George="4" Fred="5"></dict>
    <a></a>
    <dict James="1" Tammy="2" Pat="3" George="4" Fred="5"></dict>

    Which is what I want but in a different format. I need the following
    output to look like this below:

    <labels>
    James="1"
    Tammy="2"
    Pat="3"
    George="4"
    Fred="5"
    </labels>
    <labels>
    James="1"
    Tammy="2"
    Pat="3"
    George="4"
    Fred="5"
    </labels>


    I am still a newbie at this. Any help would be great.

    Thanks in advance.
    icedgar, Mar 2, 2005
    #1
    1. Advertising

  2. icedgar

    Guest

    I'm still somewhat new to this myself, but I just ran this version of
    the stylesheet through Xalan and got your desired results:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput method="xml"/>
    <xsl:eek:utput indent="yes"/>

    <xsl:template match="/">
    <xsl:apply-templates select="AAA/dict/dict/*"/>
    </xsl:template>

    <xsl:template match="AAA/dict/dict/*">
    <xsl:if test="a">
    <xsl:text>
    </xsl:text>
    <labels>
    <xsl:text>
    </xsl:text>
    <xsl:apply-templates select="a"/>
    <xsl:text>
    </xsl:text>
    </labels>
    </xsl:if>
    </xsl:template>

    <xsl:template match="a">
    <xsl:text> </xsl:text>
    <xsl:value-of select="." />
    <xsl:text>="</xsl:text>
    <xsl:value-of select="following-sibling::*[1]" />
    <xsl:text>"
    </xsl:text>
    </xsl:template>

    </xsl:stylesheet>

    I believe if you try it you will get what you wanted.

    -Greg
    , Mar 3, 2005
    #2
    1. Advertising

  3. icedgar

    icedgar Guest

    Thanks Greg.. that worked great!! Much appreciated. I was having a
    hell of a time using the <xsl:text> flag. It kept moving all the data
    around. This will help me with my formatting in the future too.
    Thanks again.
    icedgar, Mar 3, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lorentz
    Replies:
    4
    Views:
    1,119
    Lorentz
    Feb 27, 2004
  2. Suvig

    XSL Date formatting

    Suvig, Apr 1, 2004, in forum: XML
    Replies:
    1
    Views:
    1,062
    Martin Honnen
    Apr 2, 2004
  3. slberry
    Replies:
    0
    Views:
    394
    slberry
    May 15, 2004
  4. Replies:
    1
    Views:
    3,583
    A. Bolmarcich
    May 27, 2005
  5. =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

    Help with XSL/XSL:FO for producing revision indicator bar in document.

    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Apr 28, 2006, in forum: XML
    Replies:
    6
    Views:
    563
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
    May 3, 2006
Loading...

Share This Page