This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Simple attribute value replacement question


this works:

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:transform xmlns:xsl = "http://www.w3.org/1999/XSL/Transform";
version = "1.0">

<xsl:template match = "petXML">
	<xsl:element name = "animalXML">
		<xsl:apply-templates select = "*"/>
	</xsl:element>
</xsl:template>

<xsl:template match = "*">
	<xsl:copy>
		<xsl:copy-of select="attribute::* | text()"/>
		<xsl:apply-templates select="*"/>
	</xsl:copy>
</xsl:template>


<xsl:template match="pet[@type='dog']">
	<xsl:copy>
		<xsl:attribute name="type">canine</xsl:attribute>
		<xsl:copy-of select="attribute::*[local-name()!='type'] |
text()"/>
		<xsl:apply-templates select="*"/>
	</xsl:copy>
</xsl:template>

</xsl:transform>


However, I'd suggest maybe moving your pet/@type conditional logic into one
named template instead of trying to handle all of the permutation with
multiple templates, thereby decentralizing your logic.

perhaps:

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:transform xmlns:xsl = "http://www.w3.org/1999/XSL/Transform";
version = "1.0">

<xsl:template match = "petXML">
	<xsl:element name = "animalXML">
		<xsl:apply-templates select = "*"/>
	</xsl:element>
</xsl:template>

<xsl:template match = "*">
	<xsl:copy>
		<xsl:copy-of select="attribute::* | text()"/>
		<xsl:apply-templates select="*"/>
	</xsl:copy>
</xsl:template>


<xsl:template match="pet">
	<xsl:copy>
		<xsl:attribute name="type">
			<xsl:call-template name="pet.type">
				<xsl:with-param name="type" select="@type"/>
			</xsl:call-template>
		</xsl:attribute>
		<xsl:copy-of select="attribute::*[local-name()!='type'] |
text()"/>
		<xsl:apply-templates select="*"/>
	</xsl:copy>
</xsl:template>

<xsl:template name="pet.type">
	<xsl:param name="type"/>
	<xsl:choose>
		<xsl:when test="$type='dog'">canine</xsl:when>
		<xsl:otherwise><xsl:value-of
select="$type"/></xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:transform>



-Jeff




-----Original Message-----
From: Regina, Paul [mailto:pregina@Engage.com]
Sent: Monday, January 28, 2002 6:40 PM
To: XSL-List@lists.mulberrytech.com
Subject: [xsl] Simple attribute value replacement question


Here is an overly simplified example of what I am trying to do. I want
to convert from one XML to another, I am having trouble changing the
attribute "type". It needs to go from the word "dog" to the word
"canine":

<?xml version="1.0"?>
<petXML>
	<pet type="dog">
		<color>black</color>
	</pet>
</petXML>

To:

<?xml version="1.0"?>
<animalXML>
	<pet type="canine">
		<color>black</color>
	</pet>
</animalXML>

I thought this xsl would do it but apparently not:

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:transform xmlns:xsl = "http://www.w3.org/1999/XSL/Transform";
version = "1.0">

<xsl:template match = "petXML">
	<xsl:element name = "animalXML">
		<xsl:apply-templates select = "*"/>
	</xsl:element>
</xsl:template>

<xsl:template match = "*">
	<xsl:apply-templates select="@type"/>
	<xsl:copy-of select = "."/>
</xsl:template>

<xsl:template match="@type[.='dog']">
		<xsl:value-of select="canine"/>
</xsl:template>

</xsl:transform>

This is what I get:
<?xml version="1.0"?><animalXML><pet
type="dog"><color>black</color></pet></animalXML>

Can someone please tell me where I am going wrong and how to do this?
Thanks!


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]