Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 14th, 2010, 09:02 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default Transform Attribute based xml to element based xml

Hi,

I use MSXML and XSLT 1.0

I am aware that there is an identity function in XSLT that will create an exact copy of the entire xml including elements, nested elements and attributes.

I was wondering if there is a similar function that would convert all attributes in an xml to elements instead.

I will have to apply this to a number of xml I have. For instance I need to change the xml below to have only elements and not attributes. I think it should be something like:

<KmrsChart>
<Series>
<id>s1</id>
<name>S1</name>
<DataPoint>
<id>A</id>
<Description>A</Description>
<AltDescription>A</AltDescription>
<X>34</X>
<Y>21</Y>
</DataPoint>
......

I would appreciate if someone could help me and shed some light.

--------------------------------------
Full XML
---------------------------------------

<KmrsChart>
<Series id="s1" name="S1">
<DataPoint id="A">
<Description>A</Description>
<AltDescription>A</AltDescription>
<X>34</X>
<Y>21</Y>
</DataPoint>
<DataPoint id="B">
<Description>B</Description>
<AltDescription>B</AltDescription>
<X>-45</X>
<Y>45</Y>
</DataPoint>
</Series>
<Series id="s2" name="S2">
<DataPoint id="G">
<Description>G</Description>
<AltDescription>G</AltDescription>
<X>21</X>
<Y>21</Y>
</DataPoint>
<DataPoint id="H">
<Description>H</Description>
<AltDescription>H</AltDescription>
<X>-23</X>
<Y>-6</Y>
</DataPoint>
</Series>

</KmrsChart>

Cheers

P
 
Old September 14th, 2010, 09:07 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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

<xsl:template match="@*">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 14th, 2010, 09:09 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The identity template looks like this:

Code:
<xsl:template match="@*|node()">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
</xsl:template>
This matches all attributes and nodes.

If you want a separate template to match attributes only then remove the "@*|" from the match attribute of the above template and add a new template which creates a new element with the name of the attribute.

Code:
<xsl:template match="@*">
  <xsl:element name="{local-name()}"><xsl:value-of select="."/></xsl:element>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old September 14th, 2010, 09:28 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Martin and Sam,

Thanks very much for your help.

I had tried this before but it did not work because I need to mantain the structure of the xml as well with all the hierarchy.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/KmrsChart">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="*">
<xsl:if test="@*">
<xsl:for-each select="@*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</xsl:if>
<xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>
 
Old September 14th, 2010, 09:36 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Martin,

I have just tried your code and it seems to have worked very well for this xml. I will try it with other xml where I have deeper levels.

Thanks very much

This is the output I got

<KmrsChart>
<Series id="s1" name="S1">
<DataPoint id="A">
<Description>A</Description>
<AltDescription>A</AltDescription>
<X>34</X>
<Y>21</Y>
</DataPoint>
<DataPoint id="B">
<Description>B</Description>
<AltDescription>B</AltDescription>
<X>-45</X>
<Y>45</Y>
</DataPoint>
<DataPoint id="C">
<Description>C</Description>
<AltDescription>C</AltDescription>
<X>-34</X>
<Y>-32</Y>
</DataPoint>
<DataPoint id="D">
<Description>D</Description>
<AltDescription>D</AltDescription>
<X>23</X>
<Y>-67</Y>
</DataPoint>
<DataPoint id="E">
<Description>E</Description>
<AltDescription>E</AltDescription>
<X>12</X>
<Y>45</Y>
</DataPoint>
</Series>
<Series id="s2" name="S2">
<DataPoint id="G">
<Description>G</Description>
<AltDescription>G</AltDescription>
<X>21</X>
<Y>21</Y>
</DataPoint>
<DataPoint id="H">
<Description>H</Description>
<AltDescription>H</AltDescription>
<X>-23</X>
<Y>-6</Y>
</DataPoint>
<DataPoint id="I">
<Description>I</Description>
<AltDescription>I</AltDescription>
<X>-34</X>
<Y>-43</Y>
</DataPoint>
</Series>
…
</KmrsChart>

<?xml version="1.0" encoding="utf-8"?>
<KmrsChart>
<Series>
<id>s1</id>
<name>S1</name>
<DataPoint>
<id>A</id>
<Description>A</Description>
<AltDescription>A</AltDescription>
<X>34</X>
<Y>21</Y>
</DataPoint>
<DataPoint>
<id>B</id>
<Description>B</Description>
<AltDescription>B</AltDescription>
<X>-45</X>
<Y>45</Y>
</DataPoint>
<DataPoint>
<id>C</id>
<Description>C</Description>
<AltDescription>C</AltDescription>
<X>-34</X>
<Y>-32</Y>
</DataPoint>
<DataPoint>
<id>D</id>
<Description>D</Description>
<AltDescription>D</AltDescription>
<X>23</X>
<Y>-67</Y>
</DataPoint>
<DataPoint>
<id>E</id>
<Description>E</Description>
<AltDescription>E</AltDescription>
<X>12</X>
<Y>45</Y>
</DataPoint>
</Series>
<Series>
<id>s2</id>
<name>S2</name>
<DataPoint>
<id>G</id>
<Description>G</Description>
<AltDescription>G</AltDescription>
<X>21</X>
<Y>21</Y>
</DataPoint>
<DataPoint>
<id>H</id>
<Description>H</Description>
<AltDescription>H</AltDescription>
<X>-23</X>
<Y>-6</Y>
</DataPoint>
<DataPoint>
<id>I</id>
<Description>I</Description>
<AltDescription>I</AltDescription>
<X>-34</X>
<Y>-43</Y>
</DataPoint>
</Series>
</KmrsChart>
 
Old February 2nd, 2016, 06:00 AM
Registered User
 
Join Date: Feb 2016
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default help us to convert xml tags using xslt transform

<?xml version="1.0" encoding="UTF-8"?><wcsxml version="4.2" whslrno="10261">
<relation name="route" columns="RouteID|RouteTypeID">
<row values="R0501|R"/>
</relation>
<relation name="order" columns="CustID|OrderNbr">
<row values="00592|00501372"/>
<row values="00592|00501373"/>
</relation>
<relation name="orderdetail" columns="CustID|OrderNbr">
<row values="00592|00501372"/>
<row values="00592|00501372"/>
<row values="00592|00501373"/>
<row values="00592|00501373"/>
</relation>
</wcsxml>
 
Old February 2nd, 2016, 07:59 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You realise you've posted on the end of a SIX year old thread, and we have no idea what you are trying to do.

Please start a new thread and explain exactly what you are trying to do, what you have tried so far and where you are stuck and we will try to help.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
balamurugan (February 2nd, 2016)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining restrictions based on element/attribute values aldwinenriquez XML 3 May 13th, 2009 03:55 AM
need html attribute based on xml attributes charles95621 XSLT 1 May 24th, 2007 05:21 PM
Filtering XML data based on differnt XML ahmed123 XSLT 5 August 11th, 2006 09:15 AM
Match element based on attribute of child? transom XSLT 1 September 11th, 2005 03:26 PM
Adding an Element with Attribute to XML file xergic Classic ASP XML 0 November 20th, 2004 08:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.