Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xsl:variable

Reply
Thread Tools

xsl:variable

 
 
Nagi Peters
Guest
Posts: n/a
 
      09-24-2003
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
 
Reply With Quote
 
 
 
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      09-24-2003
In article <(E-Mail Removed)>,
Nagi Peters <(E-Mail Removed)> wrote:

% 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)?
--

Patrick TJ McPhee
East York Canada
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Nagi Peters
Guest
Posts: n/a
 
      09-24-2003
Hi Patric
On Wed, 24 Sep 2003 17:15:30 +0200 (MEST), Patrick TJ McPhee wrote:
> <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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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
 
Reply With Quote
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      09-25-2003
In article <(E-Mail Removed)>,
Nagi Peters <(E-Mail Removed)> wrote:

[...]

% <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.
--

Patrick TJ McPhee
East York Canada
(E-Mail Removed)
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      09-26-2003
(E-Mail Removed) (Patrick TJ McPhee) writes:

> In article <(E-Mail Removed)>,
> Nagi Peters <(E-Mail Removed)> wrote:
>
> [...]
>
> % <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
 
Reply With Quote
 
Mike Brown
Guest
Posts: n/a
 
      09-26-2003
> % <xsl:template match="chapter[@id=$chapter_id]">
>
> 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>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments