This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Namespaces.


Alejandro,

>Can someone send me some examples about namespaces in xsl templates ?
>
>I have rules with the same name aplying to different tags (i.e. I have title
>for the page and title for content, and I want something like <page:title>
>and <content:title>.

Are you sure you're after namespaces here?  The purpose of namespaces is to
mix two XML vocabularies within the same document.  A classic example would
be if you were embedding some MathML within an HTML document: all the HTML
elements would be in the HTML namespace and all the MathML elements would
be in the MathML namespace.

From the brief description that you give, I think that you have something
like:

<page>
  <title>The page's title</title>
  <content>
    <title>The content's title</title>
    ...
  </content>
</page>

In other words, the rules that you're applying are about whether a 'title'
element is a child of a 'page' element or a child of a 'content' element.
If that's the case, then you can use the xsl:template match expression to
differentiate between the two:

<xsl:template match="page/title">
  <!-- this matches the page's title -->
  ...
</xsl:template>

<xsl:template match="content/title">
  <!-- this matches the content's title -->
  ...
</xsl:template>

If you really are after namespaces, then your XML source has to define and
use them as well as your XSLT.  So you might have:

<doc xmlns:page="page-namespace" xmlns:content="content-namespace">
  ...
  <page:title>Title in the 'page' namespace</page:title>
  <content:title>Title in the 'content' namespace</content:title>
  ...
</doc>

Within your XSLT, you need to define the same namespaces as those that
appear within your XML source, as well as the XSLT namespace itself.  [You
don't have to use the same prefixes as in the XML source, though sometimes
it makes things easier to understand if you do.]  The namespaces are
usually defined by putting namespace declarations on the xsl:stylesheet
start tag:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:page="page-namespace"
                xmlns:content="content-namespace">
...
</xsl:stylesheet>

Then, within your stylesheet, you refer to any elements within that
namespace using the namespace prefix.  So, to have different templates for
page:title and content:title, you'd use:

<xsl:template match="page:title">
  <!-- matches 'title' elements in 'page' namespace -->
  ...
</xsl:template>

<xsl:template match="content:title">
  <!-- matches 'title' elements in 'content' namespace -->
  ...
</xsl:template>

And similarly for any select expressions or tests that you use: all the
XPaths will use the qualified (i.e. prefixed) names.

I hope that this helps, but do provide more details of what you're trying
to do if you need more detailed examples.

Cheers,

Jeni

Jeni Tennison
http://www.jenitennison.com/




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]