Forum Moderators: open

Message Too Old, No Replies

xsl transformation problem

         

carternc

12:19 pm on Aug 4, 2006 (gmt 0)

10+ Year Member



I need to transform the following XML as illustrated in the example at the bottom. Any help would be greatly appreciated. I'm fairly new to XSL and I've tried unsuccessfully to develop the right XSL to do the job.

Original:
<?xml version="1.0" encoding="utf-8"?>
<dataset xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<metadata>
<item name="Product Id"/>
<item name="Product Type"/>
<item name="Product Name"/>
</metadata>
<data>
<row>
<value>1</value>
<value>Car</value>
<value>Honda</value>
</row>
<row>
<value>2</value>
<value>Motorcycle</value>
<value>Harley</value>
</row>
</data>
</dataset>

Desired Output
<?xml version="1.0" encoding="utf-8"?>
<dataset xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<row>
<Product Id>1</Product Id>
<Product Type>Car</Product Type>
<Product Name>Honda</Product Name>
</row>
<row>
<Product Id>2</Product Id>
<Product Type>Motorcycle</Product Type>
<Product Name>Harley</Product Name>
</row>
</dataset>

johnl

10:52 pm on Aug 13, 2006 (gmt 0)

10+ Year Member



hi,
I think this transformation is not possible because the names of the items contain spaces (eg. "Product Type") and you want to use these names as element names. To my knowledge it is not allowed to use spaces in element names. You first must decide if it is acceptable for the transformation to delete the spaces (eg. "ProductType") or replace them by underscore or whatever.

carternc

3:28 pm on Aug 16, 2006 (gmt 0)

10+ Year Member



Yes, eliminating the spaces (or replacing with underscores) would be okay.

carternc

11:52 am on Aug 17, 2006 (gmt 0)

10+ Year Member



One other important point:

I need to handle the metadata/items dynamically. Meaning that there could be any number of metadata/items (it's not always 3 as shown in the example).

I've been trying to figure this out, but no luck yet. I've resorted to using programming code (.Net) to parse and rewrite the xml, but if it's possible to do this using XSL, I'd like to handle it that way.

Thanks in advance for your help.

carternc

1:50 pm on Aug 18, 2006 (gmt 0)

10+ Year Member



Nevermind...I figured it out.

johnl

4:15 pm on Aug 20, 2006 (gmt 0)

10+ Year Member



would be interesting to see your solution.^
if it is acceptable to substitute spaces by underscores, one could use:


<?xml version="1.0"?>
<!-- [webmasterworld.com...] -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no" encoding="utf-8"/>

<xsl:template match="/">
<xsl:element name="row">
<xsl:element name="dataset">
<xsl:apply-templates/>
</xsl:element>
</xsl:element>
</xsl:template>

<xsl:template match="row">
<xsl:for-each select="value">
<xsl:variable name="try"><xsl:value-of select="position()"/></xsl:variable>
<xsl:variable name="myvar">
<xsl:value-of select="translate(../../../metadata/item[position()=$try]/@name, ' ', '_')"/>
</xsl:variable>
<xsl:element name="{$myvar}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>