473,508 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[XSLT] How to establish a context node without <xsl:for-each>

I have an XSLT script collection that converts messed up XML to usuable XML.
Instead of generic template matching, I want to establish a context node for
some branches so that I don't have to rely on absolute path. Here's a
simplified version of my work:
[root.xsl]:
<xsl:include href="common.xsl"/>

<xsl:template match="/">
<patientDisk>
<xsl:call-template name="patientData"/>
</patientDisk>
</xsl:template>

==================================

[patientData.xsl]:

<xsl:template name="patientData">
<birthyear>
<xsl:value-of
select="document('rpt_pat.xml')//parameter[@name='PatientBirthYear']"/>
</birthyear>
<gender>
<xsl:value-of
select="document('rpt_pat.xml')//parameter[@name='PatientGender]"/>
</gender>
</xsl:template>
Instead of specifying document('rpt_pat.xml') every time using absolute
paths, I could have used <xsl:for-each select=document('rpt_pat.xml')>.
However, I'd like to avoid using this syntax because there's only one
instance of document('rpt_pat.xml') node. Using <xsl:for-eachcan lead to
whoever that reads my XSLT scripts into thinking that there are multiple
instances of the document('rpt_pat.xml') node, adding confusion to the
already huge chaos.

I'd like to avoid <xsl:template match="(something)"because some tags have
the same names in the messy XML files that I'm trying to parse, and they can
be distinguished only by observing the hieracy (which branch that XML tag is
under).

Can anybody suggest a neat way to do that? Thanks in advance.

-- Hoi
Nov 17 '08 #1
4 11522
Hoi Wong wrote:
<xsl:template name="patientData">
<birthyear>
<xsl:value-of
select="document('rpt_pat.xml')//parameter[@name='PatientBirthYear']"/>
</birthyear>
<gender>
<xsl:value-of
select="document('rpt_pat.xml')//parameter[@name='PatientGender]"/>
</gender>
</xsl:template>
Instead of specifying document('rpt_pat.xml') every time using absolute
paths, I could have used <xsl:for-each select=document('rpt_pat.xml')>.
However, I'd like to avoid using this syntax because there's only one
instance of document('rpt_pat.xml') node. Using <xsl:for-eachcan lead to
whoever that reads my XSLT scripts into thinking that there are multiple
instances of the document('rpt_pat.xml') node, adding confusion to the
already huge chaos.
I don't think there is a way to change the current node without using
apply-templates or for-each. If you want to shorten the code then using
a variable could do e.g.
<xsl:template name="patientData">
<xsl:variable name="pat_doc" select="document('rpt_pat.xml')"/>
<birthyear>
<xsl:value-of
select="$pat_doc//parameter[@name='PatientBirthYear']"/>
</birthyear>
<gender>
<xsl:value-of
select="$pat_doc//parameter[@name='PatientGender]"/>
</gender>
</xsl:template>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 17 '08 #2
In article <gf**********@news.stanford.edu>,
Hoi Wong <wo*****@stanford.eduwrote:
>Instead of specifying document('rpt_pat.xml') every time using absolute
paths, I could have used <xsl:for-each select=document('rpt_pat.xml')>.
However, I'd like to avoid using this syntax because there's only one
instance of document('rpt_pat.xml') node. Using <xsl:for-eachcan lead to
whoever that reads my XSLT scripts into thinking that there are multiple
instances of the document('rpt_pat.xml') node, adding confusion to the
already huge chaos.
Apply-templates and for-each are the only constructs that let you change
the context node.

I recommend using for-each with a comment to indicate that its only
purpose is to set the context.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Nov 17 '08 #3
Hoi Wong wrote:
Instead of specifying document('rpt_pat.xml') every time using absolute
paths,
as others have said you can't change the current node without for-each
or apply-templates, etc, however you could use a variable
<xsl:variable name="rpt" select="document('rpt_pat.xml')"/>
then you can use $rpt///parameter[@name='PatientBirthYear']

In xslt2 I'd do
<xsl:variable name="rpt" select="document('rpt_pat.xml')"/>
<xsl:key name="rpt" match="parameter" use="@name"/>
then
<xsl:template name="patientData">
<birthyear>
<xsl:value-of select="key('rpt','PatientBirthYear',$rpt)"/>
</birthyear>
<gender>
<xsl:value-of select="key('rpt','PatientGender',$rpt)"/>
</gender>
</xsl:template>

The third argument of key doesn't exactly change the current node but it
changes the effective node used for determing the key lookup, which has
more or less the same effect here.

David

--
http://dpcarlisle.blogspot.com
Nov 18 '08 #4
"David Carlisle" <da********@dcarlisle.demon.co.ukwrote in message
news:Mn*******************@newsfe01.ams2...
Hoi Wong wrote:
>Instead of specifying document('rpt_pat.xml') every time using absolute
paths,

as others have said you can't change the current node without for-each or
apply-templates, etc, however you could use a variable
<xsl:variable name="rpt" select="document('rpt_pat.xml')"/>
then you can use $rpt///parameter[@name='PatientBirthYear']

In xslt2 I'd do
<xsl:variable name="rpt" select="document('rpt_pat.xml')"/>
<xsl:key name="rpt" match="parameter" use="@name"/>
then
<xsl:template name="patientData">
<birthyear>
<xsl:value-of select="key('rpt','PatientBirthYear',$rpt)"/>
</birthyear>
<gender>
<xsl:value-of select="key('rpt','PatientGender',$rpt)"/>
</gender>
</xsl:template>

The third argument of key doesn't exactly change the current node but it
changes the effective node used for determing the key lookup, which has
more or less the same effect here.

David

--
http://dpcarlisle.blogspot.com
Thanks for the hint.

Unfortunately, the software that I'm using (namely MATLAB and the orphaned
proprietary data converter from the vendor) only support XSLT 1.0.

In fact, I was using the variables trick before hitting this forum so I'll
stick to that because it's easier to debug than switching nodes.

Cheers,
Hoi
Nov 21 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3783
by: Rainer Herbst | last post by:
Hi *, please help me solving the following problem: I have an XML document containing elements like <dir:directory xmlns:dir="http://apache.org/cocoon/directory/2.0" name="pressemitteilungen"...
1
3265
by: j erickson | last post by:
with the following xsl and xml file, the display of the gif file with the <image/url> tag works. However, the gif file in the <description> tag using the name attribute "src" won't make the correct...
1
15065
by: Matt | last post by:
For example, how to represent hyperlink in XSL? I want to add hyperlink in XSL. i.e. I need to generate <A HREF="http://mypage.html">home page</A> in HTML. I tried the following approaches but...
10
3795
by: Tjerk Wolterink | last post by:
The following code does not work: <xsl:variable name="width" select="form:image-width"/> <xsl:if test="$width>$max_image_width"> <xsl:variable name="width" select="$max_image_width"/> </xsl:if>...
2
1724
by: websls | last post by:
I tried to do this : <xsl:if test="ToutCompris"> some output </xsl:if> ToutCompris is a boolean element in my XML file My problem is the output is parse even when ToutCompris is false I...
3
2450
by: Andy Dingley | last post by:
>From a thread over in c.i.w.a.h "RFC: From XHTML to HTML via XSLT" http://groups.google.co.uk/group/comp.infosystems.www.authoring.html/msg/f112c230061ffe86 As is well-known, the XSLT HTML...
4
2428
by: mark4asp | last post by:
I'm getting a problem with this code and I think the offending linke is : <xsl:if test="$folder = 'Search'"> I want to test the value of the Folder element for a value of precisely "Search"...
8
5636
by: Hoi Wong | last post by:
With the XSLT 1.0 engine that I was forced to use, I have to parse old XML scripts where the number (to be parsed and saved into $EPISODE_NUMBER_RAW) that I want to parse is written with a comma...
6
2230
by: Olagato | last post by:
I need to transform this: <urlset xmlns="http://www.google.com/schemas/sitemap/0.84"> <url> <loc>http://localhost/index.php/index./Paths-for-the-extreme-player</ loc> </url> <url>...
2
2596
Dormilich
by: Dormilich | last post by:
Hi, my goal is to have a stylesheet, where I can change the encoding attribute of the <xsl:output> element (one shall have utf-8 and the other latin-1). is there any way to set this via a...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.