xsl:variable

Discussion in 'XML' started by Nagi Peters, Sep 24, 2003.

  1. Nagi Peters

    Nagi Peters Guest

    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
    Nagi Peters, Sep 24, 2003
    #1
    1. Advertising

  2. In article <>,
    Nagi Peters <> 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
    Patrick TJ McPhee, Sep 24, 2003
    #2
    1. Advertising

  3. Nagi Peters

    Nagi Peters Guest

    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"
    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
    Nagi Peters, Sep 24, 2003
    #3
  4. In article <>,
    Nagi Peters <> 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
    Patrick TJ McPhee, Sep 25, 2003
    #4
  5. Nagi Peters

    Micah Cowan Guest

    (Patrick TJ McPhee) writes:

    > In article <>,
    > Nagi Peters <> 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
    Micah Cowan, Sep 26, 2003
    #5
  6. Nagi Peters

    Mike Brown Guest

    > % <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>
    Mike Brown, Sep 26, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karsten Weinert
    Replies:
    3
    Views:
    1,007
    Karsten Weinert
    Jul 5, 2004
  2. MC
    Replies:
    1
    Views:
    7,253
    David Carlisle
    Mar 24, 2005
  3. Replies:
    1
    Views:
    3,598
    A. Bolmarcich
    May 27, 2005
  4. Afshar Mohebbi
    Replies:
    5
    Views:
    6,060
    Afshar Mohebbi
    Jun 26, 2006
  5. Replies:
    2
    Views:
    9,264
    Martin Honnen
    Sep 5, 2006
Loading...

Share This Page