[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Namespaces.

Subject: Re: Namespaces.
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 30 Oct 2000 11:47:07 -0400 (EST)
what namespace is title in
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.