tags:

views:

3095

answers:

3

Hi,

I want to create the following element:

<exercises xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mySchema.xsd">

If I use something like this:

<xsl:element name="excercises">
<xsl:attribute name="xmlns:xsi" namespace="http://www.w3.org/2001/XMLSchema-instance"/&gt;

Then it creates soemthing like this:

<excercises xp_0:xsi="" xmlns:xp_0="http://www.w3.org/2001/XMLSchema-instance"&gt;

Which doesnt look similar to what i want...

+5  A: 

Try the following instead:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@* | node()">
     <xsl:apply-templates select="xml"></xsl:apply-templates>
    </xsl:template>

    <xsl:template match="xml">
     <xsl:element name="exercises">
      <xsl:attribute name="xsi:noNamespaceSchemaLocation">mySchema.xsd</xsl:attribute>
      some value
     </xsl:element>
    </xsl:template>
</xsl:stylesheet>

The key concern is to declare the xsi namespace in the declaration.

I've just made up the template match on just to test.

HTH
Kev

Kev
Since the msxsl alais isn't used it might be better if you just removed it, it would make the example clearer
AnthonyWJones
yeah....I just cleaned up the MS cruft
Kev
thx a lot, and give your cat a huggy
Dr. Hfuhruhurr
LOL...thanks. :)
Kev
+1  A: 

Here is how this can be done:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 exclude-result-prefixes="xsi">
    <xsl:output omit-xml-declaration="yes"/>
    <!--                                   -->
    <xsl:template match="/">
      <exercises  xsi:noNamespaceSchemaLocation="mySchema.xsd"/>
    </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any source XML document (not used), the wanted result is produced:

<exercises xsi:noNamespaceSchemaLocation="mySchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

It is not necessary to use <xsl:attribute> in your case, however if necessary, it can be used without any problem:

    <xsl:attribute name="xsi:noNamespaceSchemaLocation">
      <xsl:value-of select="'mySchema.xsd'"/>
    </xsl:attribute>

Do note that it is a good practice to simply define the necessary namespaces at the <xsl:stylesheet> element so that they can readily be (re)used everywhere they are needed. THis is especially useful, if a given namespace will be needed on more than one generated element or attribute.

In this case it is also good to specify all such prefixes in the value of the exclude-result-prefixes attribute so that the namespaces will not be automatically propagated on all literal result elements.

Dimitre Novatchev
Why would you suppress the xsi result prefix?
AnthonyWJones
@AnthonyWJones: If you do not include the prefix "xsi" in the list of values for the "exclude-result-prefixes" attribute, every literal result element will be output as having this namespace node. This is the sole purpose of the e.r.p attr. Read here: http://www.w3.org/TR/xslt#literal-result-element
Dimitre Novatchev
+1  A: 

You could simply use:-

<exercises xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mySchema.xsd">

Directly in your XSL, that would work, you only really need xsl:element if can't hard code the tag name. Similarly with attributes you can add them directly unless you need to make conditional.

AnthonyWJones