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
Jim NeffSubject: context and referencing question
Author: Jim Neff
Date: 23 Nov 2004 04:37 PM
I am very new to xslt and have a question in regards to context and referencing.

Here is a snip of my code:

<xsl:template match="claim">

<xsl:apply-templates select="$field_definitions">

<xsl:sort select="record/field_no" order="ascending"/>

<xsl:with-param name="current_context" select="."/>
<xsl:with-param name="record_type">CA0</xsl:with-param>

</xsl:apply-templates>

<xsl:text>&#xA;</xsl:text>

</xsl:template>

<xsl:template match="records">

<xsl:param name="current_context"/>
<xsl:param name="record_type"/>

<xsl:for-each select="record">

<xsl:if test="@type = $record_type">

<xsl:value-of select="$current_context/{content}"/>

</xsl:if>

</xsl:for-each>

</xsl:template>

The value of $field_definitions is :

<records>
<record type="CA0">

<field_no>1</field_no>
<starting_position>1</starting_position>
<length>3</length>
<justification>left</justification>
<content></content>
<default_value>CA0</default_value>
<data_type>text</data_type>

</record>
<record type="CA0">

<field_no>2</field_no>
<starting_position>4</starting_position>
<length>2</length>
<justification></justification>
<content></content>
<default_value>&#x20;&#x20;</default_value>
<data_type>text</data_type>

</record>
</records>

What I want to happen is in the line:

<xsl:value-of select="$current_context/{content}"/>

I want to dynamically choose the element to the variable $current_context.

If I hard-code in the element name it works just fine. The reference "content" in curly brackets is supposed to be a reference to the <record><content> element.

So I want the text in the <record><content> element to tell me which element from the $current_context variable to use.

Does that make sense? Sorry, but I'm new and my head is about to explode with all this Xpath stuff.

Can someone direct to the answer on how to dynamically reference an element?

Thanks,
Jim

Posttop
Jim NeffSubject: context and referencing question
Author: Jim Neff
Date: 24 Nov 2004 12:57 PM
Ok, I received a BIG hint from Michael Kay.

Here is the answer to my problem:

select='$current_context/*[name() = current()/content]'

What this allows me to do is dynamically reference an element. In this code we are within a for-each loop where "content" contains the name of a element in the lookup variable $current_context.

What I learned is when you use a predicate the context within the predicate is whatever is directly outside of it (to the left of your predicate). So the name() function is in reference to the elements within the $current_context variable and not the context of the template I am currently in.

I needed to use current() to tell the predicate that "content" will be in reference to the parent node which has context in the current template. Otherwise it will try to find "content" within the $current_context variable.

Thanks again to Michael Kay and a host of websites I visisted just like this one.

--Jim Neff

 
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.