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

Re: [xsl] How to take a QName value and make it an attribute?


Subject: Re: [xsl] How to take a QName value and make it an attribute?
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Wed, 01 Aug 2012 16:42:15 +0100

It depends rather whether the transformation is schema-aware.

If it's a schema-aware transformation, and <fault> is annotated as an xs:QName, you want:

    <xsl:template match="fault">
        <xsl:copy>
            <xsl:variable name="q" as="xs:QName" select="." />
            <xsl:attribute name="{local-name-from-QName(q)}" namespace="namespace-uri-from-QName(q)">blah</xsl:attribute>
            <xsl:value-of select="." />
        </xsl:copy>
    </xsl:template>

If it's not schema-aware, then you want:

    <xsl:template match="fault">
        <xsl:copy>
            <xsl:variable name="q" as="xs:QName" select="resolve-QName(., .)" />
            <xsl:attribute name="{local-name-from-QName(q)}" namespace="namespace-uri-from-QName(q)">blah</xsl:attribute>
            <xsl:value-of select="." />
        </xsl:copy>
    </xsl:template>

Michael Kay
Saxonica


On 01/08/2012 16:24, Costello, Roger L. wrote:
Hi Folks,

I want to transform the <fault> element in this document:

<Test xmlns:soap="http://www.soap.org">
     <fault>soap:client</fault>
</Test>

Note that the value of the <fault> element is

soap:client

which is a QName.

I want to transform the <fault> element to this:

<fault soap:client="blah">soap:client</fault>

That is, make the QName value an attribute and assign it the string "blah."

After transformation the XML document is to be this:

<Test xmlns:soap="http://www.soap.org">
     <fault soap:client="blah">soap:client</fault>
</Test>

I tried this code:

     <xsl:template match="fault">
         <xsl:copy>
             <xsl:variable name="QName" select="." />
             <xsl:attribute name="{$QName}">blah</xsl:attribute>
             <xsl:value-of select="." />
         </xsl:copy>
     </xsl:template>

But it produces this error message:

Undeclared prefix in attribute name: soap

What is the proper way to accomplish the desired transformation please?

/Roger


Current Thread
Keywords
xml