Newbie XML transformation question using XSL

I

icedgar

I have just started working with XSL to transform XSL to XLM docs. I
am sure for most of you this is pretty easy and I know I am missing
something small. Here is a sample of my XML below:

<CD>
<key>Track ID</key>
<integer>37</integer>
<key>Name</key>
<string>Elevation</string>
<key>Artist</key>
<string>U2</string>
<key>Composer</key>
<string>Bono</string>
</CD>

I want to trasnform this into:

<CD
Track ID=37
Name=Elevation
Artist=U2
Composer=Bono>

I don't have a problem using XPath and for-each commands to extract the
data out of the XML. The trouble I am having is taking each <key> and
assigning it to it's trailing element.

Also does anyone know how I can format the XML as above with all the
elements within the CD tag?

Thanks for help out the newbie.
 
J

Joris Gillis

Tempore 19:17:11 said:
<CD>
<key>Track ID</key>
<integer>37</integer>
<key>Name</key>
<string>Elevation</string>
<key>Artist</key>
<string>U2</string>
<key>Composer</key>
<string>Bono</string>
</CD>

I want to transform this into:

<CD
Track ID=37
Name=Elevation
Artist=U2
Composer=Bono>
Hi,

the above is not well-formed xml; i'll assume you rather want an output like this:

<CD Track_ID="37" Name="Elevation" Artist="U2" Composer="Bono"/>

Such output can be obtained by applying the following stylesheet to the input XML:

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

<xsl:template match="CD">
<xsl:copy>
<xsl:apply-templates select="key"/>
</xsl:copy>
</xsl:template>

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

</xsl:stylesheet>


regards,
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top