[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] How to give an attribute a QName value, with the appropriate prefix?


Subject: Re: [xsl] How to give an attribute a QName value, with the appropriate prefix?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 27 Aug 2012 19:22:05 -0400

Reading Gerrit's note I realize that in my answer I was mixing XSLT with XSD in the declaration of the element.

His way of using prefix-from-QName() is correct, mine is not. I should have taken the time to test it, then I would have realized I was answering the wrong question.

. . . . . . . . Ken

At 2012-08-27 16:41 -0400, I wrote:
At 2012-08-27 20:37 +0000, Costello, Roger L. wrote:
Hi Folks,

I want to transform this:

<xs:attribute name="test" />

to this:

<xs:attribute name="test" type="xs:anySimpleType" />

Here is the code that I tried:

<xsl:template match="xs:attribute">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:attribute name="type"><xsl:value-of select="QName('http://www.w3.org/2001/XMLSchema', 'anySimpleType')" /></xsl:attribute>
<xsl:sequence select="node()" />
</xsl:copy>
</xsl:template>


Unfortunately that code produces this:

<xs:attribute name="test" type="anySimpleType" />

Notice that there is no prefix on anySimpleType.

What's the best way to accomplish my objective?

I want the prefix on anySimpleType to match the prefix on the <attribute> element, whether it be xs: or xsd: or anything else.

Retrieve it from the namespace axis:


(untested; assumes the default namespace is not the XSD namespace)
<xsl:attribute name="{name(namespace::*[.='http://www.w3.org/2001/XMLSchema'])]}:anySimpleType"
namespace="http://www.w3.org/2001/XMLSchema">
<xsl:copy-of select="node()"/>
</xsl:attribute>


or in XSLT 2.0:

(untested)
<xsl:attribute name="{prefix-from-QName(node-name(.))}:anySimpleType"
     namespace="http://www.w3.org/2001/XMLSchema">
  <xsl:copy-of select="node()"/>
</xsl:attribute>

Remember that node-name(.) gives the QName of the name of the current node, not the name string of the name of the current node.

I hope this helps.

. . . . . . . . . Ken


--
Public XSLT, XSL-FO, UBL and code list classes in Europe -- Oct 2012
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/
G. Ken Holman                   mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal


Current Thread
Keywords