Subject: Re: [xsl] Finding the ancestor that declared a namespace? From: David N Bertoni/Cambridge/IBM <david_n_bertoni@xxxxxxxxxx> Date: Mon, 5 Aug 2002 14:59:09 -0700 |
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 "Roger L. Costello" <costello@xxxxxxxxx> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Sent by: owner-xsl- cc: "Costello,Roger L." <costello@xxxxxxxxx>, (bcc: David N Bertoni/Cambridge/IBM) list@xxxxxxxxxxxxxxxxxxx Subject: [xsl] Finding the ancestor that declared a namespace? com 08/05/2002 02:12 PM Please respond to xsl-list 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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Urgent - Assigning <xsl:v, Aparna Konduri | Thread | Re: [xsl] Finding the ancestor that, David N Bertoni/Camb |
RE: [xsl] Grouping examples make my, Macaulay,Malcolm (US | Date | RE: [xsl] XSL FO resource needed, Roland Neilands |
Month |