[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Finding the ancestor that declared a namespace?

Subject: Re: Finding the ancestor that declared a namespace?
From: David N Bertoni/Cambridge/IBM <david_n_bertoni@xxxxxxxxxx>
Date: Mon, 5 Aug 2002 16:23:15 -0700
select ancestor



Hi Roger,

OK, after I sent this, I realized it just gets the closest ancestor where
the namespace is in scope, which is probably not what you want.  Since an
element gets a copy of its parent's namespace nodes, finding out where the
declaration actually exists in the input document is not possible in the
general case.  (At least I think it isn't!)

Is there some particular problem you're trying to solve with this, or are
you really just interested in finding out where the namespace declaration
exists?

Dave



                                                                                                                                                 
                      David N                                                                                                                    
                      Bertoni/Cambridge/IBM              To:      xsl-list@xxxxxxxxxxxxxxxxxxxxxx                                                
                      <david_n_bertoni@xxxxxxx           cc:      (bcc: David N Bertoni/Cambridge/IBM)                                           
                      com>                               Subject: Re:  Finding the ancestor that declared a namespace?                      
                      Sent by: owner-xsl-                                                                                                        
                      list@xxxxxxxxxxxxxxxxxxx                                                                                                   
                      com                                                                                                                        
                                                                                                                                                 
                                                                                                                                                 
                      08/05/2002 02:59 PM                                                                                                        
                      Please respond to xsl-list                                                                                                 
                                                                                                                                                 
                                                                                                                                                 







Hi Roger,

In the XPath data model, namespaces are not attributes, so searching for
attributes will not work.  In addition, I'm not sure you want to look for
the namespace prefix.  Wouldn't it be better to look for a namespace
declaration that matches the namespace URI of the bk:Book element in
question?  Here's a sample of what I'm talking about:

   <?xml version="1.0"?>
   <xsl:stylesheet
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0"
     xmlns:bk="http://www.book-category.com"
     exclude-result-prefixes="bk">

   <xsl:template match="/">
     <out>
       <xsl:for-each select="//bk:Book">
         <xsl:call-template name="find-ancestor-with-nsdecl">
           <xsl:with-param name="node" select="."/>
         </xsl:call-template>
       </xsl:for-each>
     </out>
   </xsl:template>

   <xsl:template name="find-ancestor-with-nsdecl">
     <xsl:param name="node" select="/.."/>

     <xsl:variable name="namespace-uri" select="namespace-uri()"/>
     <xsl:variable name="ns-elmt" select="$node/ancestor::*[namespace::* =
   $namespace-uri][1]"/>

     <xsl:if test="$ns-elmt">
       <xsl:text>Found the element "</xsl:text>
       <xsl:value-of select="name($ns-elmt)"/>
       <xsl:text>" with a namespace declaration for "</xsl:text>
       <xsl:value-of select="$namespace-uri"/>
       <xsl:text>"</xsl:text>
     </xsl:if>

   </xsl:template>

   </xsl:stylesheet>

I'm only using for-each as a quick way of selecting the starting node(s).
Running against this document:

   <?xml version="1.0"?>
   <bk:BookStore xmlns:bk="http://www.amazon.com">
      <bk:Category xmlns:bk="http://www.book-category.com">
          <bk:Book/>
      </bk:Category>
   </bk:BookStore>

Yields the following result:

   <?xml version="1.0" encoding="UTF-8"?>
   <out>Found the element "bk:Category" with a namespace declaration for
   "http://www.book-category.com"</out>

Hope that helps...

Dave

...

Hi Folks,

I would like to see if someone has an elegant solution to this problem:

Suppose that my stylesheet template is situated at an element with a
namespace qualifier, e.g.,

   <bk:Book>

I would like to find the element which declared the bk: namespace
qualifier.  Further, I want the first ancestor element that declared
bk:  For example, with this document:

<bk:BookStore xmlns:bk="http://www.amazon.com">
   <bk:Category xmlns:bk="http://www.book-category.com">
       <bk:Book>

The XPath expression should give me the bk:Category element.  Here's my
attempt (which didn't work):

<xsl:variable name="ns-elmt"
              select="ancestor::*[name(@.)='xmlns:bk'][1]"/>

This yielded no value for ns-elmt.  Any ideas?  /Roger


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.