xsl:variable

N

Nagi Peters

Hello

I try to tranform a XML Document in php/Sablotron with the following XSL
Stylesheet.
I tried to write "@id='1'" with the variable chapter_id.

So it works:
<xsl:variable name="chapter_id">1</xsl:variable>
<xsl:template match="chapter[@id='1']">
snipp..
</xsl:template>

But with
<xsl:template match="chapter[@id='$chapter_id']">
or
<xsl:template match="chapter[@id=$chapter_id]">
it doesn't.
These way
<xsl:template match="chapter[@id='<xsl:value-of select="$chapter_id"/>']">
and
<xsl:template match="chapter[@id=<xsl:value-of select="$chapter_id"/>]">
it doesn't work either.

How i have to write it that it is correct

Thanks for your help
Nagi
 
P

Patrick TJ McPhee

% So it works:
% <xsl:variable name="chapter_id">1</xsl:variable>
% <xsl:template match="chapter[@id='1']">
% snipp..
% </xsl:template>

% But with
% <xsl:template match="chapter[@id='$chapter_id']">

This will compare @id to the string $chapter_id

% <xsl:template match="chapter[@id=$chapter_id]">

This should work. Could you post a complete, but minimal, example
(data and stylesheet)?
 
N

Nagi Peters

Hi Patric
<xsl:template match="chapter[@id=$chapter_id]">
This should work. Could you post a complete, but minimal, example
(data and stylesheet)?

Here the xml:
<book>
<chapter id="1" label="1">
</chapter>
</book>

And the xsl:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" indent="yes" encoding="ISO-8859-1" />
<xsl:variable name="chapter_id">1</xsl:variable>
<xsl:template match="chapter[@id=$chapter_id]">
<xsl:value-of select="@label"/>
</xsl:template>
</xsl:stylesheet>

Tested on WinXP:
Tested with Sablotron (in php4.3.3).
Errror: match pattern contains a variable reference Code: 23

And Saxon 6.5.3:
Error at xsl:template on line 5 of file:/c:/temp/vartest.xsl:
The match pattern in xsl:template may not contain references to variables
Transformation failed: Failed to compile stylesheet. 1 error detected.

Same with <xsl:template match="chapter[@id='1']"> Works fine in both.

Any idea?

Thanks

Nagi
 
P

Patrick TJ McPhee

[...]

% <xsl:template match="chapter[@id=$chapter_id]">

% Tested on WinXP:
% Tested with Sablotron (in php4.3.3).
% Errror: match pattern contains a variable reference Code: 23
%
% And Saxon 6.5.3:
% Error at xsl:template on line 5 of file:/c:/temp/vartest.xsl:
% The match pattern in xsl:template may not contain references to variables
% Transformation failed: Failed to compile stylesheet. 1 error detected.

Obviously there's no way to do this with those processors, then. I'm not
a standards lawyer, but I just quickly looked over the grammar for
patterns and I don't see any reason for disallowing variables in the
predicate. In fact it says `Predicates in a pattern can use arbitrary
expressions just like predicates in a location path.'

libxml does allow this, and your example works. I don't have anything
else installed on this machine so I can't comment beyond that.
 
M

Micah Cowan

[...]

% <xsl:template match="chapter[@id=$chapter_id]">

% Tested on WinXP:
% Tested with Sablotron (in php4.3.3).
% Errror: match pattern contains a variable reference Code: 23
%
% And Saxon 6.5.3:
% Error at xsl:template on line 5 of file:/c:/temp/vartest.xsl:
% The match pattern in xsl:template may not contain references to variables
% Transformation failed: Failed to compile stylesheet. 1 error detected.

Obviously there's no way to do this with those processors, then. I'm not
a standards lawyer, but I just quickly looked over the grammar for
patterns and I don't see any reason for disallowing variables in the
predicate. In fact it says `Predicates in a pattern can use arbitrary
expressions just like predicates in a location path.'

In §5.3 ("Defining Template Rules"), para 1, it says, "It is an
error for the value of the match attribute to contain a
VariableReference". Guess that explains it.

-Micah
 
M

Mike Brown

% said:
libxml does allow this

....then it has a bug that should be reported.

A workaround that doesn't violate any rules:

<xsl:template match="chapter">
<xsl:if test="@id=$chapter_id">
...
</xsl:if>
</xsl:template>
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top