There are two XPath 2.0 functions designed to create expanded QName values xs:QName values:
There is also a set of XPath 2.0 functions that are designed to work on xs:QName values:
We'll take a quick look at these functions here. The fn:resolve-QName FunctionThis function creates an xs:QName value when you pass it a string, using the namespaces for a given element: fn:resolve-QName( $qname as xs:string, $element as element) as xs:QName The return value is an xs:QName value that depends on the form of the string you pass to iteither prefix:local-name or local-name . The prefix you pass to this function is resolved using the in-scope namespaces of the element you pass it. Here's an example. Say that the element node in $element has one namespace node, which is bound to the prefix xdata . In that case, this expression returns an xs:QName with local name "data" that is not in any namespace: fn:resolve-QName("data", $element) On the other hand, the following expression returns an xs:QName value whose namespace URI is given by the URI of the namespace node matching the prefix xdata and whose local name is data : fn:resolve-QName("xdata:data", $element) The fn:expanded-QName FunctionThis function creates an xs:QName value when you pass it a namespace URI and a local name: fn:expanded-QName( $URI as xs:string, $Local as xs:string) as xs:QName The return value is an xs:QName that has the namespace URI given in $URI and the local name given in $Local . If $URI is an empty string, the return value has no namespace. For example, this expression returns an xs:QName value with the namespace URI http://www.XPathCorp.com/actors and the local name carygrant : fn:expanded-QName("http://www.XPathCorp.com/actors", "carygrant") The fn:get-local-name-from-QName FunctionThe fn:get-local-name-from-QName function lets you retrieve the local name from an xs:QName value: fn:get-local-name-from-QName( $srcval as xs:QName?) as xs:string? The return value is an xs:string representing the local part of $srcval . Here's an examplein this case, we're creating an xs:QName value with fn:expanded-QName("http://www.XPathCorp.com/actors", "carygrant") , and then extracting its local name. You can see how this works in ch11_07.xsl (Listing 11.7). Listing 11.7 Using fn:get-local-name-from-QName ( ch11_07.xsl )<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xdt="http://www.w3.org/2003/05/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:template match="/"> <xsl:value-of select="get-local-name-from-QName( expanded-QName('http://www.XPathCorp.com/actors', 'carygrant'))"/> </xsl:template> </xsl:stylesheet> And here is the result, showing that we have indeed been able to extract the local name of this xs:QName value: <?xml version="1.0" encoding="UTF-8"?> carygrant The fn:get-namespace-uri-from-QName FunctionThis function retrieves the namespace URI from an xs:QName value: fn:get-namespace-uri-from-QName( $srcval as xs:QName?) as xs:string? The return value is the namespace URI for $srcval as an xs:string . You can see an example in ch11_08.xsl (Listing 11.8), where we're extracting the namespace URI from the xs:QName we created in the preceding example: fn:expanded-QName("http://www.XPathCorp.com/actors", "carygrant") . (Note that get-namespace-uri-from-QName was previously named get-namespace-from-QName , which is the way it's implemented in Saxon, so we'll call it that in this example's code.) Listing 11.8 Using fn:get-namespace-from-QName ( ch11_08.xsl )<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xdt="http://www.w3.org/2003/05/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:template match="/"> <xsl:value-of select="get-namespace-from-QName( expanded-QName('http://www.XPathCorp.com/actors', 'carygrant'))"/> </xsl:template> </xsl:stylesheet> And here is the result, showing the namespace URI we want: <?xml version="1.0" encoding="UTF-8"?> http://www.XPathCorp.com/actors The fn:get-namespace-uri-for-prefix FunctionThis function returns the namespace URI for a given prefix: fn:get-namespace-uri-for-prefix( $element as element, $prefix as xs:string) as xs:string? The return value is an xs:string that is the namespace URI of the in-scope namespace for $element that matches the namespace prefix in $prefix . For example, if the element node in $element has an in-scope prefix xdata that corresponds to http://www.XPathCorp.com , the following would return "http://www.XPathCorp.com". fn:get-namespace-uri-for-prefix($element, "xdata") The fn:get-in-scope-prefixes FunctionThis function returns a sequence of the in-scope prefixes for an element. Here's how you use this function: fn:get-in-scope-namespaces( $element as element) as xs:string* To put this to work, we might add a new prefix, xdata , to our planetary data document like this: <?xml version="1.0"?> <planets xmlns:xdata="http://www.XPathCorp.com"> <planet> <name>Mercury</name> <mass units="(Earth = 1)">.0553</mass> <day units="days">58.65</day> <radius units="miles">1516</radius> <density units="(Earth = 1)">.983</density> <distance units="million miles">43.4</distance> <!--At perihelion--> </planet> . . . Now you can find the in-scope prefixes for the <planets> element as you see in ch11_09.xsl (Listing 11.9). (Saxon knows this function by its previous name, get-in-scope-namespaces , so we'll leave it like that for this example's code.) Listing 11.9 Using fn:get-in-scope-namespaces ( ch11_09.xsl ) <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xdt="http://www.w3.org/2003/05/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:template match="/planets"> <xsl:value-of select="get-in-scope-namespaces(.)" separator=", "/> </xsl:template> </xsl:stylesheet> Here are the results, showing the in-scope prefixes for the <planets> element, which are the xdata and the default xml prefixes: <?xml version="1.0" encoding="UTF-8"?> xdata, xml |