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

Converting attribute value qnames to URIs

Subject: Converting attribute value qnames to URIs
From: "bob@xxxxxxxx" <bob@xxxxxxxx>
Date: Thu, 25 Mar 2004 12:16:17 -0500
testing document example
I wrote the stylesheet below to convert qnames in attribute values to full
URIs. Following it is a short test document I used for input. It covers a
few cases, but probably not all it should. I'm curious to hear any
suggestions anyone has to improve the stylesheet, or if this has already
been done better somewhere elsewhere. (Google searches on things like "XSLT
qnames URLs" don't  focus the number of hits very much.) I think it makes a
nice demo of a little-used XPath axis.

thanks,

Bob DuCharme          www.snee.com/bob           <bob@  
snee.com>  "The elements be kind to thee, and make thy
spirits all of comfort!" Anthony and Cleopatra, III ii

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<!-- qname2uri.xsl: convert namespace prefixes in attribute
                    values to their associated URIs. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
					 version="1.0">

  <xsl:template match="@*[contains(.,':')]">
	 <!-- For any attributes that have a colon in their value... -->

	 <xsl:variable name="nsprefix">
		<xsl:value-of select="substring-before(.,':')"/>
	 </xsl:variable>

	 <xsl:variable name="nsURI">
		<!-- URI that the prefix maps to: first ancestor with a
			  namespace node whose name() = the namespace prefix. -->
		<xsl:variable name="nsNode">
		  <xsl:value-of select=
           "ancestor-or-self::*[1]/namespace::*[name() = $nsprefix]"/>
		</xsl:variable>
		<xsl:choose>
		  <xsl:when test="$nsNode=''">
			 <!-- Uncomment the following xsl:text element to 
			      flag undeclared prefix -->
			 <!-- <xsl:text>NO-URI-DECLARED-FOR-PREFIX:</xsl:text> -->
			 <xsl:value-of select="$nsprefix"/>
			 <xsl:text>:</xsl:text>
		  </xsl:when>
		  <xsl:otherwise>
			 <xsl:value-of select="$nsNode"/>
		  </xsl:otherwise>
		</xsl:choose>
	 </xsl:variable>

	 <!-- Add attribute to result tree, substituting URI for prefix. -->
	 <xsl:attribute name="{name()}">
		<xsl:value-of select="$nsURI"/>
		<xsl:value-of select="substring-after(.,':')"/>
	 </xsl:attribute>

  </xsl:template>

  <!-- Copy anything not covered by that first template rule. -->
  <xsl:template match="@*|node()">
	 <xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	 </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

<!-- end of qname2uri.xsl -->

<!-- beginning of sample input document -->

<a xmlns:sn="http://www.snee.com/ns/whatever#">
  <b>this is a test</b>
  <b attr1="sn:blah">Second b element.</b>
  <b attr1="xx:blah">Third b element.</b> <!-- No declaration for xx. -->
  <c xmlns:sn="http://www.example.com/"> <!-- Redeclared prefix. -->
	 <d color="red" direction="north">    <!-- No colons in these values. -->
		<x attr2="sn:foo">nested namespace</x>
	 </d>
  </c>
</a>

<!-- end of sample input document -->


--------------------------------------------------------------------
mail2web - Check your email from the web at
http://mail2web.com/ .

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.