XPath question: [ ] and attributes

Discussion in 'XML' started by Karsten Weinert, Oct 12, 2003.

  1. Hello,
    can someone please explain to me why the one stylesheet given below
    works, but the other not?

    <!-- This stylesheet works -->
    <?xml version="1.0"?>

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="/">
    <html><head></head><body>
    <xsl:apply-templates/>
    </body></html>
    </xsl:template>

    <xsl:template match="extern">
    <xsl:variable name="vPart" select="@part"/>
    <xsl:apply-templates select="document(@file)/content/*[name()=$vPart]"/>
    </xsl:template>

    </xsl:stylesheet>

    <!-- This stylesheet does not work -->
    <?xml version="1.0"?>

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="/">
    <html><head></head><body>
    <xsl:apply-templates/>
    </body></html>
    </xsl:template>

    <xsl:template match="extern">
    <xsl:apply-templates select="document(@file)/content/*[name()=@part]"/>
    </xsl:template>

    </xsl:stylesheet>

    The stylesheet work on this file:

    <?xml version="1.0"?>

    <content>
    <extern file="extern_resource.xml" part="parta"/>
    </content>

    and use this file also:

    <!-- extern_resource.xml -->

    <?xml version="1.0"?>
    <content>
    <parta>This is the content part a</parta>
    <partb>This is the content part b</partb>
    </content>
     
    Karsten Weinert, Oct 12, 2003
    #1
    1. Advertising

  2. In article <>,
    Karsten Weinert <> wrote:

    % can someone please explain to me why the one stylesheet given below
    % works, but the other not?

    Inside a predicate, the context node is set to the node against which
    the predicate is being tested.

    % <xsl:template match="extern">
    % <xsl:variable name="vPart" select="@part"/>

    Here, vPart is set to the value of the attribute `part' of the element
    `extern'.

    % <xsl:apply-templates select="document(@file)/content/*[name()=$vPart]"/>
    % </xsl:template>

    And here, it's tested against the name of each element which is a child
    of `content' in the the file given by the `file' attribute of `extern'.

    % <xsl:apply-templates select="document(@file)/content/*[name()=@part]"/>

    Here' the attribute `part' of each element which is a child of `content'
    in the file given by the `file' attribute of `extern' is tested against
    the name of the same element. That is, `part' is not taken from `extern',
    but from the node being tested by the predicate.


    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Oct 13, 2003
    #2
    1. Advertising

  3. Just to add to the precise explanation of Patrick:

    You can avoid having to use a variable. Instead of:

    > <xsl:apply-templates select="document(@file)/content/*[name()=$vPart]"/>


    use

    <xsl:apply-templates
    select="document(@file)/content/*[name()=current()/@part]"/>


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL



    "Karsten Weinert" <> wrote in message
    news:...
    > Hello,
    > can someone please explain to me why the one stylesheet given below
    > works, but the other not?
    >
    > <!-- This stylesheet works -->
    > <?xml version="1.0"?>
    >
    > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    version="1.0">
    >
    > <xsl:template match="/">
    > <html><head></head><body>
    > <xsl:apply-templates/>
    > </body></html>
    > </xsl:template>
    >
    > <xsl:template match="extern">
    > <xsl:variable name="vPart" select="@part"/>
    > <xsl:apply-templates select="document(@file)/content/*[name()=$vPart]"/>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > <!-- This stylesheet does not work -->
    > <?xml version="1.0"?>
    >
    > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    version="1.0">
    >
    > <xsl:template match="/">
    > <html><head></head><body>
    > <xsl:apply-templates/>
    > </body></html>
    > </xsl:template>
    >
    > <xsl:template match="extern">
    > <xsl:apply-templates select="document(@file)/content/*[name()=@part]"/>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > The stylesheet work on this file:
    >
    > <?xml version="1.0"?>
    >
    > <content>
    > <extern file="extern_resource.xml" part="parta"/>
    > </content>
    >
    > and use this file also:
    >
    > <!-- extern_resource.xml -->
    >
    > <?xml version="1.0"?>
    > <content>
    > <parta>This is the content part a</parta>
    > <partb>This is the content part b</partb>
    > </content>
     
    Dimitre Novatchev, Oct 13, 2003
    #3
  4. (Patrick TJ McPhee) wrote in message news:<bmctqp$ge9$>...

    > % <xsl:apply-templates select="document(@file)/content/*[name()=@part]"/>
    >
    > Here' the attribute `part' of each element which is a child of `content'
    > in the file given by the `file' attribute of `extern' is tested against
    > the name of the same element. That is, `part' is not taken from `extern',
    > but from the node being tested by the predicate.


    Ah, it looks for an attribut "part" of the <parta> or <partb> tags,
    which do not exists. Thanks a lot,

    Karsten.
     
    Karsten Weinert, Oct 13, 2003
    #4
  5. (Patrick TJ McPhee) wrote in message news:<bmctqp$ge9$>...

    > % <xsl:apply-templates select="document(@file)/content/*[name()=@part]"/>
    >
    > Here' the attribute `part' of each element which is a child of `content'
    > in the file given by the `file' attribute of `extern' is tested against
    > the name of the same element. That is, `part' is not taken from `extern',
    > but from the node being tested by the predicate.


    Ah, it looks for an attribut "part" of the <parta> or <partb> tags,
    which do not exists. Thanks a lot,

    Karsten.
     
    Karsten Weinert, Oct 13, 2003
    #5
    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. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,993
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,443
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. girish
    Replies:
    2
    Views:
    10,240
    Peter Flynn
    Aug 6, 2004
  4. Mavis
    Replies:
    1
    Views:
    522
    =?iso-8859-1?q?Fl=E1vio_Barata?=
    Aug 8, 2006
  5. Sharat  Koya
    Replies:
    1
    Views:
    391
    Martin Honnen
    Jul 27, 2007
Loading...

Share This Page