xsl:variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nagi Peters

    #1

    xsl:variable

    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_i d">1</xsl:variable>
    <xsl:template match="chapter[@id='1']">
    snipp..
    </xsl:template>

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

    How i have to write it that it is correct

    Thanks for your help
    Nagi
  • Patrick TJ McPhee

    #2
    Re: xsl:variable

    In article <1f6cfc12742493 a1dcb3b2b592205 fec@ID-25423.user.dfnc is.de>,
    Nagi Peters <nagi@gmx.de> wrote:

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

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

    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
    ptjm@interlog.c om

    Comment

    • Nagi Peters

      #3
      Re: xsl:variable

      Hi Patric
      On Wed, 24 Sep 2003 17:15:30 +0200 (MEST), Patrick TJ McPhee wrote:[color=blue]
      > <xsl:template match="chapter[@id=$chapter_id]">
      > This should work. Could you post a complete, but minimal, example
      > (data and stylesheet)?[/color]

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

      And the xsl:
      <?xml version="1.0" encoding="ISO-8859-1"?>
      <xsl:styleshe et version="1.0"
      xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" indent="yes" encoding="ISO-8859-1" />
      <xsl:variable name="chapter_i d">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

      Comment

      • Patrick TJ McPhee

        #4
        Re: xsl:variable

        In article <e79ff5b401e5cc f6a6d7c26845ee4 867@ID-25423.user.dfnc is.de>,
        Nagi Peters <nagi@gmx.de> 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
        ptjm@interlog.c om

        Comment

        • Micah Cowan

          #5
          Re: xsl:variable

          ptjm@interlog.c om (Patrick TJ McPhee) writes:
          [color=blue]
          > In article <e79ff5b401e5cc f6a6d7c26845ee4 867@ID-25423.user.dfnc is.de>,
          > Nagi Peters <nagi@gmx.de> 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.'[/color]

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

          -Micah

          Comment

          • Mike Brown

            #6
            Re: xsl:variable

            > % <xsl:template match="chapter[@id=$chapter_id]">[color=blue]
            >
            > libxml does allow this[/color]

            ....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=$chap ter_id">
            ...
            </xsl:if>
            </xsl:template>


            Comment

            Working...