Namespaces in output documents

Namespace details may need to be added to the elements and attributes output by the XSLT processor. Indeed, this is vital when the created document contains elements from more than one namespace.

Multiple namespaces

It is a simple matter to include output elements in the stylesheet that have different prefixes, though it is of course necessary to include namespace declarations for each one.

When the output document contains elements from more than one namespace, the root element usually needs to contain multiple namespace declarations. All namespace declarations in the stylesheet are automatically copied to the output document, except for the XSLT namespace declaration and any others that are explicitly excluded (see below).

Avoiding namespaces

It is sometimes useful to be able to create output documents that do not use namespaces. Some simple XML-based applications may not be aware of the namespaces standard, and be 'hardwired' to expect specific element and attribute names. Such applications will not recognize names that include a namespace prefix. Namespaces also make XML documents harder to read, and more verbose.

The easy way to omit element name prefixes is to avoid using prefixes for these elements within the stylesheet (to make the output set the default namespace).

However, even the default namespace has a namespace declaration within the stylesheet, and normally this declaration is copied to the output. In this scenario, though, the declaration is not wanted. The Exclude Result Prefixes attribute can be used to suppress the namespace declaration. The value of this attribute consists of one or more space-separated prefix names, but in this case there is no prefix name (because it is the default). For the default namespace, the keyword '#default' is used:

<xsl:stylesheet xmlns="OUTnamespace"
                ...
                exclude-result-prefixes="#default">
  ...
</xsl:stylesheet>

The namespace declaration for the XSLT elements is not copied to output, but all other declarations in the Stylesheet element would normally be copied, including any that have been included just to refer to elements in the source document (as described above). These namespace declarations can also be suppressed in the output document using the Exclude Result Prefixes attribute:

<xsl:stylesheet
          xmlns:xsl="..."
          xmlns="OUTnamespace"
          xmlns:acme="ACMEnamespace"
          exclude-result-prefixes="#default acme">
  ...
  <xsl:template match="acme:widget">
    <OUT-WIDGET><xsl:apply-templates/></OUT-WIDGET>
  </xsl:template>
  ...
</xsl:stylesheet>


   <OUT>
      <OUT-WIDGET>...</OUT-WIDGET>
   </OUT>

Note that, because declarations are otherwise automatically copied to output from the stylesheet itself, attempts to copy-through the namespace declaration on the root element of the source document, using general techniques for copying-through attribute values, are not applicable in this special case.

It is important to appreciate that the Exclude Result Prefixes attribute alone cannot be used to create an output document that does not use namespaces. It excludes the namespace declaration but does not (despite its name) suppress prefixes attached to element names within the stylesheet. It is also necessary to use the default namespace for output elements.

Embedded namespaces

A large document may contain small fragments that conform to a different namespace, and the namespace declaration can appear on the 'root' element of each fragment, instead of on the root element of the entire document. The Element element has an optional Namespace attribute, which holds the URL of the namespace. An XSLT processor should be able to generate a prefix to use for the contents, such as 'ns1':

<template match="acme:widget">
  <element name="OUT-WIDGET" namespace="OutURL">
    <apply-templates/>
  </element>
</template>


   <ns1:OUT-WIDGET xmlns:ns1="OutURL">
      <ns1:NAME>...</ns1:NAME>
      ...
   </ns1:OUT-WIDGET>

The Attribute element may also use the Namespace attribute. If a document contains only a very small number of attributes from another namespace, this is a practical option. Two attributes are actually created. First, the named attribute is created and given the stated value, as usual, but the name of this attribute includes a generated prefix. In addition, a namespace declaration attribute is also created on the element, including the URL given in the Namespace attribute, and the generated prefix. For example, if a document occasionally contained a Currency attribute that conformed to the (fictional) 'Registered Currencies' format, and this format were to be assigned the namespace 'currencyURL', the following Namespace attribute would be appropriate:

<template match="price">
  <element name="PRICE">
    <attribute name="CURRENCY" namespace="currencyURL">
    <apply-templates/>
  </element>
</template>


   <WIDGET>
     <PRICE ns1:CURRENCY="EN-pounds"
            xmlns:ns1="currencyURL">134</PRICE>
     ...
   </WIDGET>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.116.159