XSL formatting help..

I

icedgar

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.
 
G

grghoward

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
 
I

icedgar

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top