Subject: [xsl] Converting attribute value qnames to URIs From: "bob@xxxxxxxx" <bob@xxxxxxxx> Date: Thu, 25 Mar 2004 12:16:17 -0500 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] XSL and FO, cknell | Thread | Re: [xsl] Converting attribute valu, David Carlisle |
RE: [xsl] XSLT 2.0 Decimal number s, Andreas L. Delmelle | Date | Re: [xsl] XSLT 2.0 Decimal number s, Jeff Kenton |
Month |