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 August 14th, 2009, 05:08 PM
Registered User
 
Join Date: Aug 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Question 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,
 
Old August 14th, 2009, 05:14 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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:
oornelas_Ca (August 14th, 2009)
 
Old August 14th, 2009, 05:29 PM
Registered User
 
Join Date: Aug 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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,
 
Old August 14th, 2009, 05:38 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old August 14th, 2009, 06:10 PM
Registered User
 
Join Date: Aug 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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,
 
Old August 14th, 2009, 07:54 PM
Registered User
 
Join Date: Aug 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Question 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>
 
Old August 15th, 2009, 06:56 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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
 
Old August 15th, 2009, 10:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stumped on XSLT template hayedid XSLT 8 December 19th, 2007 02:14 PM
XSLT: ONE template to transform all the elements Behl_Neha XSLT 8 December 15th, 2007 07:31 PM
XSLT Template Issue saravanan.k XSLT 2 March 14th, 2007 06:41 AM
XSLT Template error again bmains XSLT 4 December 19th, 2003 11:17 AM
XSLT - First Time Matching a Template highlife XSLT 4 July 25th, 2003 07:50 AM





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