XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Neal WaltersSubject: Curly braces to substitute value?
Author: Neal Walters
Date: 23 May 2006 07:13 PM
I'm trying to help someone map this data from a post in a Biztalk forum:


<ItemDetails xmlns="">
<Orders>
<ID>Order01</ID>
<Branch1>
<Type01>
<Name>Name1</Name>
</Type01>
</Branch1>
</Orders>
<Orders>
<ID>Order01</ID>
<Branch1>
<Type02>
<ItemCode>Item01</ItemCode>
</Type02>
</Branch1>
</Orders>
<Orders>
<ID>Order01</ID>
<Branch1>
<Type03>
<CDate>20060517</CDate>
</Type03>
</Branch1>
</Orders>
<Orders>
<ID>Order02</ID>
<Branch1>
<Type01>
<Name>Name2</Name>
</Type01>
</Branch1>
</Orders>
<Orders>
<ID>Order02</ID>
<Branch1>
<Type02>
<ItemCode>Item02</ItemCode>
</Type02>
</Branch1>
</Orders>
<Orders>
<ID>Order02</ID>
<Branch1>
<Type03>
<CDate>20060518</CDate>
</Type03>
</Branch1>
</Orders>
</ItemDetails>

Desired output is:
<Output>
<Order>
<ID>Order01</ID>
<Name>Name01</Name>
<ItemCode>Item01</ItemCode>
</Order>

then same for second order.

</Output>

So far this is what I have:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<OutputOrders>

<xsl:for-each select="/ItemDetails/Orders[Branch1/Type01/Name]">
<Order>
<Name>
<xsl:value-of select="Branch1/Type01/Name"/>
</Name>
<xsl:variable name="myXpathItemCode">//ItemDetails/Orders[Branch/Type01/Name='"<xsl:value-of select="ID"/>']Branch1/Type02/ItemCode]</xsl:variable>
<debugXpath>
<xsl:value-of select="$myXpathItemCode"/>
</debugXpath>
<ItemCode>
<xsl:value-of select="{$myXpathItemCode}"/>
</ItemCode>
</Order>
</xsl:for-each>
</OutputOrders>
</xsl:template>
</xsl:stylesheet>

I'm trying to figure out how to get the variable to substitute the actual xpath. It thought it had something to do with the curly braces, but I get compile error on the curly braces. If I remove them, I just get the xpath printed in the output.

Thanks,
Neal Walters
http://Biztalk-Training.com - Learn Biztalk Faster
http://Sharepoint-Training.com - End-User Videos

Postnext
Tony LavinioSubject: Curly braces to substitute value?
Author: Tony Lavinio
Date: 24 May 2006 09:02 AM
You can't build and evaluate xpaths dynamically unless you use an
engines vendor-specific extensions - and not all engines have those.

In this case, there is another way. First store the value that
you want Name to match on in a variable, and then in your [ ]
expression, but the actual test.

<xsl:variable name="code" select="....."/>
<xsl:value-of select="//ItemDetails/Orders[Branch/Type01/Name = $code]/....</value-of>

(BTW, the xpath you were using is not well-formed; it's got more
]'s than ['s and seems to be missing a slash after the first ].)

Postnext
Neal WaltersSubject: Curly braces to substitute value?
Author: Neal Walters
Date: 24 May 2006 10:49 AM
I'm having trouble understanding all your last post.
This is what I'm trying now. The "Name" element is matching fine, I'm trying to match on ItemCode.

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

<xsl:template match="/">
<OutputOrders>

<xsl:for-each select="/ItemDetails/Orders[Branch1/Type01/Name]">
<Order>
<xsl:variable name="ID">
<xsl:value-of select="ID"/>
</xsl:variable>
<ID>
<xsl:value-of select="$ID"/>
</ID>
<Name>
<xsl:value-of select="Branch1/Type01/Name"/>
</Name>
<xsl:variable name="myXpathItemCode">//ItemDetails/Orders[ID='<xsl:value-of select="ID"/>']/Branch1/Type02/ItemCode</xsl:variable>
<DebugXpath>
<xsl:value-of select="$myXpathItemCode"/>
</DebugXpath>

<xsl:variable name="myXpathItemValue">
<xsl:value-of select="//ItemDetails/Orders[ID='$ID']/Branch1/Type02/ItemCode"/>
</xsl:variable>
<ItemCode>
<xsl:value-of select="$myXpathItemValue"/>
</ItemCode>
</Order>
</xsl:for-each>
</OutputOrders>
</xsl:template>
</xsl:stylesheet>

Also, I'm trying to figure out when the curly braces are needed if ever.

Thanks,
Neal Walters
http://Biztalk-Training.com - Learn Biztalk Faster

Posttop
Ivan PedruzziSubject: Curly braces to substitute value?
Author: Ivan Pedruzzi
Date: 24 May 2006 06:34 PM
Hi Neal,

Curly braces, the official name is AVT (Attribute Value Template) serve to assign attribute value in literal XML; for example

<Record id="{myid}"/>

You cannot use AVT inside XSLT instructions, more generally as Tony said, XSLT doesn't allow to evaluate dynamic XPath expressions.

Going back to the example:

<xsl:variable name="myXpathItemValue">
<xsl:value-of select="//ItemDetails/Orders[ID='$ID']/Branch1/Type02/ItemCode"/>
</xsl:variable>

1) Always use select unless you need to create a separate fragment.
2) The predicates should be [ID=$ID]

<xsl:variable name="myXpathItemValue" select="//ItemDetails/Orders[ID=$ID]/Branch1/Type02/ItemCode"/>

Please take a look to the attached solution

Hope this helps
Ivan Pedruzzi
Stylus Studio Team


Documentitemdetails.xsl

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.