Jump to content

xsl:attribute w/variable for 'name' attribute?


Audball
 Share

Recommended Posts

I have some XML input like this:

<Foo>  <Bar>Baz</Bar></Foo>

What I'd like to generate is (don't worry about the xs:import for A:B:C:Baz):

<xs:element ref="Baz:Baz" xmlns:Baz="A:B:C:Baz"/>

Here's what I have so far:

<xsl:template match="Foo">    <xsl:element name="xs:element">        <xsl:attribute name="ref">            <xsl:value-of select="Bar"/>            <xsl:text>:</xsl:text>            <xsl:value-of select="Bar"/>        </xsl:attribute>        <xsl:variable name="bazNamespace">            <xsl:text>xmlns:</xsl:text>            <xsl:value-of select="Bar"/>        </xsl:variable>        <xsl:attribute name="HowDoIDoThis-{$bazNamespace}-DoesNotWork">            <xsl:text>A:B:C:</xsl:text>            <xsl:value-of select="Bar"/>        </xsl:attribute>    </xsl:element></xsl:template>

I guess I could manually build the element and attributes with a buch of <xsl:text>, but that seems crummy.Any ideas?

Link to comment
Share on other sites

Is the <xsl:text> really necessary? Shorten yourself a bit. If it is, then sorry. Put it back :) .As for this problem, I saw someone here that sucseeded in doing what you want with the only difference that he did it with <xsl:element>. I think it should work here as well.What you need to do is to turn the variable into parameter instead and use the string() function to select the parameter. So try using this:

<xsl:template match="Foo">   <xsl:element name="xs:element">       <xsl:attribute name="ref">           <xsl:value-of select="Bar"/>:<xsl:value-of select="Bar"/>       </xsl:attribute>       <xsl:parameter name="bazNamespace">           xmlns:<xsl:value-of select="Bar"/>       </xsl:parameter>       <xsl:attribute name="{string($bazNamespace)}">           A:B:C:<xsl:value-of select="Bar"/>       </xsl:attribute>   </xsl:element></xsl:template>

Link to comment
Share on other sites

Thanks for the responce, boen_robot. No, the <xml:text> wasn't necessary at all -- it was a byproduct of having started learning XSLT yesterday :) Ultimately, I ended up NOT using a variable, and instead using the XSLT 2.0 <xsl:namespace> feature, thusly:

    <xsl:template match="Foo">         <xsl:element name="xs:element">             <xsl:attribute name="ref">                 <xsl:value-of select="concat(Bar, ':', Bar)"/>             </xsl:attribute>             <xsl:namespace name="{Bar}">                 <xsl:value-of select="concat('A:B:C:', Bar)"/>             </xsl:namespace>         </xsl:element>     </xsl:template>

I'll put your example as a comment in the transform, just in case I need to revert back to XSLT 1.0 features :)Cheers,David

Link to comment
Share on other sites

Is XSLT 2.0 even supported in today's browsers :) ? I thought it's still a candidate reccomendation and nothing yet supports it or at least, not it's features.

Link to comment
Share on other sites

I'm not worried about browsers, as the associated .XSD, .XSL, and .XML files are all intended for our own custom application. All they need to do is be edited/validated/tested in any decent XML editor (I'm using Oxygen 7.1 for now), and be loadable (only the .XMLs) via Xerces-C in C++ code. The generated schemas by the .XSL transforms are only ever going to be used for validation against .XML files created in the .XML editor -- it's completely a data-driven input/output approach, and I'm using XSLT literally for transforming, NOT for display purposes.

Is XSLT 2.0 even supported in today's browsers :) ? I thought it's still a candidate reccomendation and nothing yet supports it or at least, not it's features.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...