XSL problem matching node containing xmlns attribute

Discussion in 'XML' started by clover2411, Jan 22, 2007.

  1. clover2411

    clover2411 Guest

    Hi there,

    Apologies if I'm posting to the wrong group; this one looked to be the
    best match.

    I'm having trouble writing a bit of XSL/XPATH and wondered if someone
    would please shed some light. My XML is generated by InfoPath and of
    the structure:

    ....
    <my:Alpha>
    <my:Beta>
    <my:Gamma>
    <div xmlns="http://www.w3.org/1999/xhtml">one line of text</div>
    <div xmlns="http://www.w3.org/1999/xhtml">another one</div>
    </my:Gamma>
    </my:Beta>
    <my:Beta>
    <my:Gamma>just the one line here
    </my:Gamma>
    </my:Beta>
    </my:Alpha>
    ....

    Note the my:Gamma node - sometimes it will contain divs separating
    pieces of text, other times just text. In my XSL I'm trying to test
    whether my:Gamma contains divs (if it does, then I can get value-of
    div, otherwise I can just get value-of my:Gamma).

    I'm trying to use the below construct:

    <xsl:choose>
    <xsl:when test="div">
    ...
    </xsl:when>
    <xsl:eek:therwise>
    ...
    </xsl:eek:therwise>
    </xsl:choose>

    This works if I get rid of the xmlns="http://www.w3.org/1999/xhtml"
    from the div tags, but I can't seem to write a valid test condition in
    the xsl:when that will find the full <div
    xmlns="http://www.w3.org/1999/xhtml"> tag. I thought <xsl:when
    test="div[@*]"> might work but it doesn't seem to, leading me to think
    that 'xmlns' isn't classified as an attribute as such. Maybe it's
    blindingly obvious but I haven't been able to find an answer!

    Many thanks in advance.
    clover2411, Jan 22, 2007
    #1
    1. Advertising

  2. clover2411 wrote:

    > <my:Gamma>
    > <div xmlns="http://www.w3.org/1999/xhtml">one line of text</div>
    > <div xmlns="http://www.w3.org/1999/xhtml">another one</div>
    > </my:Gamma>


    > <xsl:choose>
    > <xsl:when test="div">


    <xsl:when test="xhtml:div" xmlns:xhtml="http://www.w3.org/1999/xhtml">


    Usually you will put the namespae declaration
    xmlns:xhtml="http://www.w3.org/1999/xhtml" on the xsl:stylesheet element.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 22, 2007
    #2
    1. Advertising

  3. clover2411 <> wrote:

    >
    > This works if I get rid of the xmlns="http://www.w3.org/1999/xhtml"
    > from the div tags, but I can't seem to write a valid test condition in
    > the xsl:when that will find the full <div
    > xmlns="http://www.w3.org/1999/xhtml"> tag. I thought <xsl:when
    > test="div[@*]"> might work but it doesn't seem to, leading me to think
    > that 'xmlns' isn't classified as an attribute as such. Maybe it's
    > blindingly obvious but I haven't been able to find an answer!


    I think that's a question of name space

    add to your <xsl:stylesheet version ...
    the following :

    xmlns:h="http://www.w3.org/1999/xhtml">

    don't put xmlns="..." in your div tags.

    but rather for example :

    <xsl:when test="//h:div[@foo = 'bar]">
    [...]

    you'll have to prefix all xhtml element with h:


    --
    Artaban de Médée
    =?ISO-8859-1?Q?Une_B=E9vue?=, Jan 22, 2007
    #3
  4. clover2411

    clover2411 Guest

    Thanks very much for your replies, I've used the below suggestion which
    works nicely (and put the namespace declaration on the xsl:stylesheet
    element). Makes sense now I see it! :)

    > <xsl:when test="xhtml:div" xmlns:xhtml="http://www.w3.org/1999/xhtml">
    >
    > Usually you will put the namespae declaration
    > xmlns:xhtml="http://www.w3.org/1999/xhtml" on the xsl:stylesheet element.


    Not sure why InfoPath generates div tags with namespace declarations on
    them in the middle of a form though, it'd be easier if it didn't!
    clover2411, Jan 23, 2007
    #4
  5. clover2411

    Andy Dingley Guest

    clover2411 wrote:
    > Not sure why InfoPath generates div tags with namespace declarations on
    > them in the middle of a form though, it'd be easier if it didn't!


    No, it will be _exactly_ as easy to work with them, however they
    appear. It's fundamental to XML that these namespacing rules don't care
    where the attribute appears, they just care what's in scope.

    How you code your XSLT is a separate problem. IMHE, always place your
    own namespaces on the XSLT root xsl:stylesheet element and always use
    an explicit namespace prefix, not the default. Although it's
    technically possible to do it the other way, it's not humanly possible
    to understand the source.
    Andy Dingley, Jan 23, 2007
    #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. CB
    Replies:
    1
    Views:
    591
    Johannes Koch
    Oct 2, 2003
  2. bearclaws
    Replies:
    3
    Views:
    576
    bearclaws
    Mar 10, 2005
  3. Replies:
    0
    Views:
    1,352
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,423
    Dimitre Novatchev
    Aug 24, 2006
  5. afshar
    Replies:
    3
    Views:
    24,251
    aljar
    May 19, 2010
Loading...

Share This Page