Simple Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike King

    Simple Question

    What is the XPath expression that selects the value attribute in the
    following xml?

    <?xml version="1.0"?>
    <root xmlns="http://a/" xmlns:a="http://a/">
    <child a:value="1"/>
    </root>


  • Joris Gillis

    #2
    Re: Simple Question

    On Fri, 18 Feb 2005 11:10:44 -0500, Mike King <emailMK@excite .com> wrote:
    [color=blue]
    > What is the XPath expression that selects the value attribute in the
    > following xml?
    >
    > <?xml version="1.0"?>
    > <root xmlns="http://a/" xmlns:a="http://a/">
    > <child a:value="1"/>
    > </root>
    >
    >[/color]
    //a:child/@a:value

    with the prefix 'a' bound to namespace 'http://a/'

    regards,
    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

    Comment

    • David Carlisle

      #3
      Re: Simple Question


      [color=blue]
      > What is the XPath expression that selects the value attribute in the
      > following xml?[/color]

      /a:root/a:child/@a:value

      where a is bound to "http://a/" either using
      xmlns:a="http://a/"
      in XSLT or via a setting in your XPath API.

      David

      Comment

      • Mike King

        #4
        Re: Simple Question

        "Joris Gillis" <roac@pandora.b e> wrote in message
        news:opsmeifdbk yf9v9r@bepv0001 ...[color=blue]
        > On Fri, 18 Feb 2005 11:10:44 -0500, Mike King <emailMK@excite .com> wrote:
        >[color=green]
        >> What is the XPath expression that selects the value attribute in the
        >> following xml?
        >>
        >> <?xml version="1.0"?>
        >> <root xmlns="http://a/" xmlns:a="http://a/">
        >> <child a:value="1"/>
        >> </root>
        >>
        >>[/color]
        > //a:child/@a:value
        >
        > with the prefix 'a' bound to namespace 'http://a/'
        >
        > regards,
        > --
        > Using Opera's revolutionary e-mail client: http://www.opera.com/m2/[/color]


        I tried that, and it didn't work. What's wrong with my XSLT.

        <?xml version="1.0"?>
        <xsl:styleshe et version="1.0"
        xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" xmlns:a="http://a/">
        <xsl:template match="@a:value ">
        found
        </xsl:template>
        </xsl:stylesheet>


        Comment

        • Joris Gillis

          #5
          Re: Simple Question

          Tempore 17:57:14, die Friday 18 February 2005 AD, hinc in foro {comp.text.xml} scripsit Mike King <emailMK@excite .com>:

          [color=blue]
          > <?xml version="1.0"?>
          > <xsl:styleshe et version="1.0"
          > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" xmlns:a="http://a/">
          > <xsl:template match="@a:value ">
          > found
          > </xsl:template>
          > </xsl:stylesheet>[/color]

          The problem here is that attribute nodes are not applied by default.
          An equivalent of the built-in template rule in XSLT1.0:
          <xsl:template match="*|/">
          <xsl:apply-templates select="node()"/>
          </xsl:template>

          In order to let your xslt work, add this template:
          <xsl:template match="*|/">
          <xsl:apply-templates select="node()| @*"/>
          </xsl:template>
          but this will likely result in other unwanted behaviour.


          regards,
          --
          Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
          "Quot capita, tot sententiae" - Terentius , Phormio 454

          Comment

          • David Carlisle

            #6
            Re: Simple Question

            [color=blue]
            > I tried that, and it didn't work. What's wrong with my XSLT.
            >
            > <?xml version="1.0"?>
            > <xsl:styleshe et version="1.0"
            > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" xmlns:a="http://a/">
            > <xsl:template match="@a:value ">
            > found
            > </xsl:template>
            > </xsl:stylesheet>
            >
            >
            >[/color]

            A template only does anything if you apply templates to the matching
            node, and that stylesheet doesn't apply templates to attribute nodes, so
            no template natching an attribute will be executed.

            the default templates for / and elements apply templates to child nodes
            (only) not to attribute or namespace nodes. This is why an empty
            stylesheet produces all element content (but not attribute content)

            add

            <xsl:template match="*">
            <xsl:apply-templates select="node()| @*"/>
            </xsl:template>

            David

            Comment

            • Mike King

              #7
              Re: Simple Question

              > A template only does anything if you apply templates to the matching[color=blue]
              > node, and that stylesheet doesn't apply templates to attribute nodes, so
              > no template natching an attribute will be executed.
              >
              > the default templates for / and elements apply templates to child nodes
              > (only) not to attribute or namespace nodes. This is why an empty
              > stylesheet produces all element content (but not attribute content)
              >
              > add
              >
              > <xsl:template match="*">
              > <xsl:apply-templates select="node()| @*"/>
              > </xsl:template>
              >
              > David[/color]

              Ok then won't the following match as a template?

              a:child/@a:value


              Comment

              • Joris Gillis

                #8
                Re: Simple Question

                On Fri, 18 Feb 2005 13:34:05 -0500, Mike King <emailMK@excite .com> wrote:
                [color=blue]
                > Ok then won't the following match as a template?
                >
                > a:child/@a:value[/color]


                No,

                The pattern '@a:value' isn't matched, so the same pattern with an extra
                restriction (only 'value' attributes with a 'child' parent) will certainly
                not match more.

                But you can do this:

                <xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
                xmlns:s="s" xmlns:w="vv" xmlns:abc="abc"
                version="1.0" xmlns:a="http://a/">

                <xsl:output method="xml" indent="yes"/>

                <xsl:template match="a:root">
                <xsl:apply-templates select="a:child/@a:value"/>
                </xsl:template>

                <xsl:template match="a:child/@a:value ">
                match
                </xsl:template>

                </xsl:stylesheet>

                --
                Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

                Comment

                • Mike King

                  #9
                  Re: Simple Question

                  > No,[color=blue]
                  >
                  > The pattern '@a:value' isn't matched, so the same pattern with an extra
                  > restriction (only 'value' attributes with a 'child' parent) will certainly
                  > not match more.
                  >
                  > But you can do this:
                  >
                  > <xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
                  > xmlns:s="s" xmlns:w="vv" xmlns:abc="abc"
                  > version="1.0" xmlns:a="http://a/">
                  >
                  > <xsl:output method="xml" indent="yes"/>
                  >
                  > <xsl:template match="a:root">
                  > <xsl:apply-templates select="a:child/@a:value"/>
                  > </xsl:template>
                  >
                  > <xsl:template match="a:child/@a:value ">
                  > match
                  > </xsl:template>
                  >
                  > </xsl:stylesheet>[/color]


                  I'm starting to understand now. XSLT is like a push-model where the
                  XSL/XSLT processor *must* be told to look for a specific pattern, not that
                  it's recursively going through the tree structure looking for a matching
                  pattern.


                  Comment

                  • Joris Gillis

                    #10
                    Re: Simple Question

                    On Fri, 18 Feb 2005 14:00:04 -0500, Mike King <emailMK@excite .com> wrote:
                    [color=blue]
                    > I'm starting to understand now. XSLT is like a push-model where the
                    > XSL/XSLT processor *must* be told to look for a specific pattern, not
                    > that it's recursively going through the tree structure looking for a
                    > matching
                    > pattern.
                    >[/color]

                    The xslt processor has to be told (with <xsl:apply-templates/>) to dive
                    further into the source tree, but there are built in templates.

                    The built-in templates make the XLST processor go through all nodes,
                    except attribute nodes.

                    --
                    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

                    Comment

                    Working...