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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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

   
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.