Using Select with multiple separate nodes in XSL

R

RanDeep

I have two nodes that both exist underneath the root node. They are
linked, however, in the sense that one of the nodes contains a copy of
an id that is used to refer to the other. However, when I try create a
param using this search critieria it can never seem to locate what I
am looking for. For example, check out the following XML file:
-------------------------
<data>
<aBlock id="1">
<tmpdata>
<stringType>Hi</stringType>
</tmpdata>
<reference>
<id>3</id>
</reference>
</aBlock>
<bBlock id="2">
<message>Wrong one</message>
</bBlock>
<bBlock id="3">
<message>Right One</message>
</bBlock>
</data>
-------------------------

with its corresponding stylesheet:
-------------------------
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title>Test</title></head>
<body>
Test output:<br/>
<xsl:for-each select="/data/aBlock">
<xsl:apply-templates mode="subData" select="."/>
</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template mode="subData" match="aBlock">
<xsl:param name="bBlockRef" select="/data/bBlock[@id='2']"/><br />
<xsl:value-of select="./reference"/><br/>
<!-- Problem Statement -->
The Message is: <xsl:value-of
select="/bBlock[@id=./reference]/message"/>
</xsl:template>
</xsl:stylesheet>
-------------------------

The result looks this:
-------------------------
Test output:

3
The Message is:
-------------------------

So the problem is that trying to match the id attribute to a subNode
in the current node isn't working. I don't know if this is because my
syntax is correct (but the preceding value-of expression shows that it
is functional) or if it is because I am trying to perform illegal
behavior in XSL, or what.

But if anyone could help me out- I would be in your debt.

Randeep Walia
 
M

Martin Honnen

RanDeep said:
I have two nodes that both exist underneath the root node. They are
linked, however, in the sense that one of the nodes contains a copy of
an id that is used to refer to the other. However, when I try create a
param using this search critieria it can never seem to locate what I
am looking for. For example, check out the following XML file:
-------------------------
<data>
<aBlock id="1">
<tmpdata>
<stringType>Hi</stringType>
</tmpdata>
<reference>
<id>3</id>
</reference>
</aBlock>
<bBlock id="2">
<message>Wrong one</message>
</bBlock>
<bBlock id="3">
<message>Right One</message>
</bBlock>
</data>
-------------------------

with its corresponding stylesheet:
-------------------------
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title>Test</title></head>
<body>
Test output:<br/>
<xsl:for-each select="/data/aBlock">
<xsl:apply-templates mode="subData" select="."/>
</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template mode="subData" match="aBlock">
<xsl:param name="bBlockRef" select="/data/bBlock[@id='2']"/><br />
<xsl:value-of select="./reference"/><br/>
<!-- Problem Statement -->
The Message is: <xsl:value-of
select="/bBlock[@id=./reference]/message"/>
</xsl:template>
</xsl:stylesheet>
-------------------------

The result looks this:
-------------------------
Test output:

3
The Message is:
-------------------------

So the problem is that trying to match the id attribute to a subNode
in the current node isn't working. I don't know if this is because my
syntax is correct (but the preceding value-of expression shows that it
is functional) or if it is because I am trying to perform illegal
behavior in XSL, or what.

But if anyone could help me out- I would be in your debt.

You could store the value in a variable and use that, here is an example
stylesheet

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title>Test</title></head>
<body>
Test output:<br/>
<xsl:for-each select="/data/aBlock">
<xsl:apply-templates mode="subData" select="."/>
</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template mode="subData" match="aBlock">
<xsl:variable name="ref" select="reference/id" />
<xsl:value-of select="$ref"/><br/>
<!-- Problem Statement -->
The Message is: <xsl:value-of
select="/data/bBlock[@id=$ref]/message"/>
</xsl:template>
</xsl:stylesheet>
 
R

RanDeep

Martin Honnen said:
RanDeep said:
I have two nodes that both exist underneath the root node. They are
linked, however, in the sense that one of the nodes contains a copy of
an id that is used to refer to the other. However, when I try create a
param using this search critieria it can never seem to locate what I
am looking for. For example, check out the following XML file:
-------------------------

<xsl:template mode="subData" match="aBlock">
<xsl:param name="bBlockRef" select="/data/bBlock[@id='2']"/><br />
<xsl:value-of select="./reference"/><br/>
<!-- Problem Statement -->
The Message is: <xsl:value-of
select="/bBlock[@id=./reference]/message"/>
</xsl:template>
</xsl:stylesheet>
-------------------------

You could store the value in a variable and use that, here is an example
stylesheet

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title>Test</title></head>
<body>
Test output:<br/>
<xsl:for-each select="/data/aBlock">
<xsl:apply-templates mode="subData" select="."/>
</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template mode="subData" match="aBlock">
<xsl:variable name="ref" select="reference/id" />
<xsl:value-of select="$ref"/><br/>
<!-- Problem Statement -->
The Message is: <xsl:value-of
select="/data/bBlock[@id=$ref]/message"/>
</xsl:template>
</xsl:stylesheet>

Using a temp variable is actually the work-around I am using
currently, but my question is, why do I have to use it at all? I tried
doing some research to find out if this was standard behavior or not
but found nothing.

I guess I am looking for the technical reason for this behavior- why
does XSL not allow attribute comparisons in select statements between
two separate nodes, and whether or not that behavior will be
consistent in future XSL versions.

Randeep
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top