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

Re: [xsl] Namespaces and XSD


Subject: Re: [xsl] Namespaces and XSD
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 18 Dec 2014 21:12:20 -0000

> Hi,
>   Ive been using DTDs with my XML for years. Now I am trying to replace one
of my DTDs with an XSD. Ive been told that the root element of the XML I am
outputting from an XSLT transform should have a namespace that corresponds to
the targetNamespace of my schema.
>
>   So far so good. I output the root element with the namespace. But now some
of the child elements are being output with xslns=. Ive tried everything I
can think of to eliminate this extra namespaces from being output. (I am
running SAXON EE and using XSLT 2.0)

This doesn't add an extra namespace, it removes (undeclares) a namespace.
>
>   Ive tried adding xpath-default-namespace= to the value of the
targetNamespace. Ive tried it with . Neither work.
>   Ive tried replacing generized template matches, using copy, with specific
templates that specify which attributes to output.
>
>   I finally removed the namespace from the root element so its not output
anymore. Now I have no extraneous namespaces in my output. But, how will the
output XML file know which schema defines it?
>
>   How can I get rid of the unwanted namespace?

Let's call it an unwanted namespace undeclaration rather than an unwanted
namespace.

The key question is, what namespace should the elements be in? When you
construct elements in your stylesheet, you decide what name to give them. The
name is a two-part thing: namespace URI plus local name. You decide what name
to give the element (URI and local parts), and the XSLT processor decides what
namespace declarations and undeclarations to emit in order to implement your
decision.

You haven't included enough of your schema to make it clear whether all the
elements should be in the target namespace of the schema, or whether only the
root element should. Common practice is to put all elements in the target
namespace, but some people do otherwise. The fact that you say the namespace
undeclaration is unwanted suggests that you want all the elements to be in the
target namespace, and the namespace undeclarations mean that they aren't. If
they aren't in the target namespace this is because you created them with the
wrong name.

You output the title element using the literal result element <title>. This
means it will go in the namespace corresponding to the default namespace of
the stylesheet. (xmlns="....."). Unfortunately you haven't shown enough of the
stylesheet for us to know what the default namespace is, but I suspect it is
null. This means you are putting the title element in no namespace. As this is
different from the namespace of the root element, Saxon has to make sure that
the title element goes in no namespace by generating a namespace
undeclaration. To avoid the namespace undeclaration, put the title element in
a namespace, for example by writing it as <title xmlns=".....">, or by adding
a default namespace declaration to the xsl:stylesheet element.

Michael Kay
Saxonica
>
>   Is there another way to reference the schema (XSD) from the XML document
without adding a namespace to the root element?
>
> Thanks,
>   Craig
>
>   Heres a snippet of the XML source file I am transforming:
> <?xml version="1.0" encoding="UTF-8"?>
> <!--Arbortext, Inc., 1988-2012, v.4002-->
> <!DOCTYPE refDictEntry PUBLIC "-//SAS//DTD authoring 13.0//EN"
"authoring.dtd">
> <refDictEntry docAlias="lefunctionsref"
> eid="p0xkrj83an7dknn1sgukpmnphcje" type="function" xml:lang="en">
> <commandLineHelpEntry
eid="p05b61n8f36dwgn1gs1h1l6biwuj">ABS</commandLineHelpEntry>
> <indexEntry includeInSyntaxIndex="yes"><?Pub Dtl?><primary><?Pub Dtl?>ABS
> function</primary></indexEntry><indexEntry><?Pub Dtl?><primary><?Pub
Dtl?>absolute
> value</primary></indexEntry>
> <title>ABS Function</title>
> <shortDescription>Returns the absolute value.</shortDescription>
> <summary>
> <category>Mathematical</category>
> </summary>
> <syntax eid="p07w43trquf74on1ct3ur17des7e">
> <syntaxSimple eid="p005cpxu5su0dkn1b6to129kl254">
> <syntaxLevel><keyword>ABS</keyword>(<userSuppliedSyntaxValue
>
link="p1shy0v9fx5303n11p04kh3zsvm3">argument</userSuppliedSyntaxValue>)</synt
axLevel>
> </syntaxSimple>
>
> Heres the title template so you can see I am doing nothing to output a
namespace:
> <xsl:template match="title[parent::refDictEntry]">
>   <title>
>     <xsl:choose>
>       <xsl:when test="string-length(substring-before(normalize-space(.),'
'))=0">
>         <xsl:apply-templates/> <!-- no word space found so assume only one
word in title -->
>       </xsl:when>
>       <xsl:otherwise>
>         <xsl:value-of select="substring-before(normalize-space(.),' ')"/>
<!-- get first word in title -->
>       </xsl:otherwise>
>     </xsl:choose>
>   </title>
> </xsl:template>
>
>
>
>   Heres a snippet of output with no namespace on the
refDictEntryCjollection root element:
> <?xml version="1.0" encoding="UTF-8"?>
> <refDictEntryCollection eid="n1ewuqnosopv1kn1vx2dx8l3rchi" xml:lang="en">
>    <refDictEntry eid="p0xkrj83an7dknn1sgukpmnphcje"
>                  type="function"
>                  xml:lang="en"
>                  docAlias="lefunctionsref">
>       <?myURI
http://support.sas.com/documentation/cdl/en/lefunctionsref/1/HTML/default/vie
wer.htm#p0xkrj83an7dknn1sgukpmnphcje.htm?>
>       <title>ABS</title>
>       <shortDescription>Returns the absolute value.</shortDescription>
>       <summary>
>          <category>Mathematical</category>
>       </summary>
>       <syntax eid="p07w43trquf74on1ct3ur17des7e">
>          <syntaxSimple eid="p005cpxu5su0dkn1b6to129kl254">
>             <syntaxLevel>
>                <keyword>
>
>
>   Heres the same snippet when I put the namespace on the root element.
Notice the title, shortDescription, summary and syntax elements now have null
namespaces.
> <?xml version="1.0" encoding="UTF-8"?>
> <refDictEntryCollection
xmlns="http://www.sas.com/xml/namespace/sas_doc_xis_refDictEntryCollection-1.
0"
>                         eid="n1ewuqnosopv1kn1vx2dx8l3rchi"
>                         xml:lang="en">
>    <refDictEntry eid="p0xkrj83an7dknn1sgukpmnphcje"
>                  type="function"
>                  xml:lang="en"
>                  docAlias="lefunctionsref">
>       <?myURI
http://support.sas.com/documentation/cdl/en/lefunctionsref/1/HTML/default/vie
wer.htm#p0xkrj83an7dknn1sgukpmnphcje.htm?>
>       <title xmlns="">ABS</title>
>       <shortDescription xmlns="">Returns the absolute
value.</shortDescription>
>       <summary xmlns="">
>          <category>Mathematical</category>
>       </summary>
>       <syntax xmlns="" eid="p07w43trquf74on1ct3ur17des7e">
>          <syntaxSimple eid="p005cpxu5su0dkn1b6to129kl254">
>             <syntaxLevel>
>                <keyword>
>
>
>   And finally heres the title element defined in the schema:
>        <xs:element name="title">
>               <xs:complexType mixed="true">
>                      <xs:choice minOccurs="0" maxOccurs="unbounded">
>                            <xs:element ref="statValue"/>
>                            <xs:element ref="subscript"/>
>                            <xs:element ref="superscript"/>
>                            <xs:element ref="userSuppliedValue"/>
>                            <xs:element ref="windowName"/>
>                      </xs:choice>
>               </xs:complexType>
>        </xs:element>
>
> XSL-List info and archive
> EasyUnsubscribe (by email)


Current Thread
Keywords