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 19th, 2010, 10:12 PM
Registered User
 
Join Date: Aug 2010
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Default Transforming attribute with several values

Hello,

i have a xml-file, that looks like that:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<aa>
	<bb id="b1" name="ex1"/>
	<bb id="b2" name="ex2"/>
	<bb id="b3" ref="b1 b2" name="ex3"/>
</aa>
the result should look like this file:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<aa>
	<cc name="ex1"/>
	<cc name="ex2" ref="//aa/ex1"/>
	<cc name="ex3" ref="//aa/ex1 //aa/ex2"/>
</aa>
My problem is that the ref attribute contains several values, which should be transformed. Each value represents an object. I tried to transform it with Saxon 9.1, but it complains if i try the following transformation:

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://example.com" version="2.0">
	<xsl:output method="xml" encoding="ISO-8859-1" />
	
	<xsl:template match="//bb">
	<bb name="./@name">
		<xsl:attribute name="ref">
			<xsl:for-each select="tokenize(./@ref, ' ')">
				//aa/<xsl:value-of select="//bb[@id=.]//@name"/>
			</xsl:for-each>
		</xsl:attribute>
	</bb>
	</xsl:template>
	
</xsl:stylesheet>
Is there a solution to this problem?
 
Old August 20th, 2010, 02:37 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

A couple of things.

Firstly, you have a lot of use of "//" - in my opinion overuse. This will always slow down your XSLT - more so the larger the XML input. Don't use it unless you need to. It's definitely not needed in the template match property.

Secondly, inside the for-each instruction the current context is an atomic value (a string), so the "//" at the start of the xpath has no meaning, because it tries to find all nodes in the current context - and you can't for this in an atom value.

Thirdly, in the [@id=.] predicate the "." refers to the current element - inside the xpath, i.e. the parent bb element you are searching, not the string value returned by the tokenize.

Taking all those points the following is an example that does what you want:

Code:
	  <xsl:variable name="aa" select="/aa"/>
	
       <xsl:template match="bb">
       <bb name="{@name}">
               <xsl:attribute name="ref">
                       <xsl:for-each select="tokenize(@ref, ' ')">
                               <xsl:text>//aa/</xsl:text>
                               <xsl:value-of select="$aa/bb[@id=current()]/@name"/>
                               <xsl:text> </xsl:text>
                       </xsl:for-each>
               </xsl:attribute>
       </bb>
       </xsl:template>
__________________
/- 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:
moggi (August 20th, 2010)
 
Old August 20th, 2010, 04:00 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Or for a more concise version

Code:
       <xsl:variable name="aa" select="/aa"/>
	
       <xsl:template match="bb">
       <bb name="{@name}"
             ref="{for $t in tokenize(@ref, ' ') 
                     return concat('//a/', $aa/bb[@id=$t]/@name}"/>
       </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:
moggi (August 20th, 2010)
 
Old August 20th, 2010, 09:22 PM
Registered User
 
Join Date: Aug 2010
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thank you. It works fine.

I used "//" because it's just a simplified example. In my project I don't know exactly where my "bb" are.
 
Old August 21st, 2010, 05:26 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It doesn't matter - you don't need the // in a match statement to match a template against a "bb" element anywhere in the input xml.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
DOM Attribute name/values BrendonMelville XML 2 February 15th, 2006 06:05 PM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
how to get values depending on the attribute vidhya XSLT 1 July 8th, 2005 03:55 AM
Matching of attribute values gb XSLT 2 February 10th, 2004 04:12 AM
different values with the same attribute name srini XSLT 0 January 21st, 2004 05:21 AM





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