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

August 14th, 2009, 05:08 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
XSLT Template
Using an XSLT Template, how can I modify all "property" elements so that these are change from this:
<property name="OnEmergency" type="System.Boolean" assembly="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">False</property>
To something like this:
<OnEmergency>False</OnEmergency>
Basically, I need to replace the element name with the value in the "name" attribute. Then, all attributes need to be removed.
Thanks,
|
|

August 14th, 2009, 05:14 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Code:
<xsl:template match="property">
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|
The Following User Says Thank You to mhkay For This Useful Post:
|
|
|

August 14th, 2009, 05:29 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Almost there
The code snippet replaces only the top level "property" items, my bad, I did not specified that there are elements within elements (in many sub-levels) that are also named "property". Also, the following error is thrown:
"Only one top level element is allowed in an XML document"
I tried adding "<root>....</root>" but did not get rid off the error message.
Thanks,
|
|

August 14th, 2009, 05:38 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Well to handle nested property elements just do xsl:apply-templates instead of xsl:value-of select=".", to recurse down the tree in the usual way.
If your output has more than one root element then outputting an element node from the match="/" template should fix it.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

August 14th, 2009, 06:10 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
First of all, thanks for the great quick replies.
Still, something is missing, here is what I used as template:
<xsl:template match="/">
<xsl:element name="{@name}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
Here is what I got:
The empty string '' is not a valid name.
==================
My Source is this:
<?xml version="1.0" encoding="utf-8"?>
<object name="Inventory">
<properties>
<property name="Summary">
<properties>
<property name="StockABC" type="System.Int32">123</property>
<property name="StockABC" type="System.Int32">223</property>
</properties>
</property>
</properties>
</object>
=======================
The desire Output is this:
<?xml version="1.0" encoding="utf-8"?>
<Inventory>
<properties>
<Summary name="Summary">
<properties>
<StockABC>123</StockABC>
<StockABC>223</StockABC>
</properties>
</Summary>
</properties>
</Inventory>
Thanks,
|
|

August 14th, 2009, 07:54 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Correction of desired Output
The desire Output is this:
<?xml version="1.0" encoding="utf-8"?>
<Inventory>
<properties>
<Summary>
<properties>
<StockABC>123</StockABC>
<StockABC>223</StockABC>
</properties>
</Summary>
</properties>
</Inventory>
|
|

August 15th, 2009, 06:56 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
The following two templates should suffice:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="property[@name] | object[@name]">
<xsl:element name="{@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

August 15th, 2009, 10:11 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
In a template with match="/", the context node is the document root. Then @name refers to an attribute of the document node, which doesn't exist, so you are trying to create an element with no name. You should only use <element name="{@name}"> when you know the context node will have a name attribute and when you know it will be a valid element name.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|
 |