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 February 7th, 2006, 03:16 PM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to default an attribute value

Given a school xml file: it contains attributes of name and district
<?xml version="1.0" encoding="utf-8"?>
<School name="Joy" district="Albany">
<Class>A</Class>
<Teacher>Dave</Teacher>
</School>

I want to have an identity copy without "Class" element and force the district attribute a value of "NY". I thought following stylesheet should work, but it fails to defalut the attribute value:

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

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


<xsl:template match="Class"/>


<xsl:template match="School[@district]">
  <xsl:copy>
    <xsl:attribute name="district">NY</xsl:attribute>
    <xsl:apply-templates select="@*|node()" />
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

That is, running the school xml file above, I would expect following:
<?xml version="1.0" encoding="utf-8"?>
<School name="Joy" district="NY">
<Teacher>Dave</Teacher>
</School>

Instead the result is:
<?xml version="1.0" encoding="utf-8"?>
<School district="Albany" name="Joy">
<Teacher>Dave</Teacher>
</School>

That's the district attribute still remains as "Albany" instead of the desired "NY". What to do?


 
Old February 8th, 2006, 09:20 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The apply-templates select="@*" is causing the district attribute to be copied, and when you write two attributes with the same name to the same element, the last one wins.

Add

<xsl:template match="@district"/>

to your stylesheet.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace an attribute with another attribute georgemeng XSLT 8 June 10th, 2008 11:04 AM
attribute Eddy de Boer XSLT 5 June 16th, 2006 07:12 AM
all attribute kfir XML 1 May 8th, 2006 09:34 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
get attribute value... clayman58 XSLT 1 August 9th, 2005 04:33 AM





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