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

Re: [xsl] namespaces problem


Subject: Re: [xsl] namespaces problem
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 31 Jul 2003 13:02:02 -0400

At 2003-07-31 18:25 +0200, Marco Contardi wrote:
I have this xml (sample XMLSpy generate from xsd)

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="out_struttura_pra.xsl"?>
<PraPermessoZTL
xmlns="http://www.saga.it/egov/esicra/importazione/xml/esicra_pra"
xmlns:esicra_import="
http://www.saga.it/egov/esicra/importazione/xml/esicra_import"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.saga.it/egov/esicra/importazione/xml/esicra_pra
PraPermessoZTL.xsd">
      <Pratica>
            <Id>2147483647</Id>
            <Oggetto>HELLO</Oggetto>
      </Pratica>
</PraPermessoZTL>

I process the xml with the following xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
</xsl:template>
</xsl:stylesheet>

The problem is that it don't work,

It work only when I delete the row "xmlns="http://....." from xml
I know that is a namespaces problem but I need the solution

Yes, this is a namespace problem.


Unprefixed elements and attributes in XPath expressions address constructs only found in no namespace. When you use the default namespace in an XML instance, your constructs are in a namespace even though there is no prefix. Therefore, in your XPath instructions in XSLT you are obliged to use a prefix, even though there is no prefix in your XML instance.

Namespace processing is not based on the prefix used, it is based on the URI associated with the elements and attributes through various techniques of using prefixes and declaring associated namespace URI strings.

The reason your stylesheet works when you remove the default namespace declaration from your instance is because you are changing all of your elements from in a namespace to no namespace and your unprefixed XPath addresses find the constructs. When you put the namespace declaration back in, you are adding the URI back onto the constructs and your unchanged XPath addresses can no longer find them. You are obliged to add prefixes to your XPath address steps in your stylesheet.

You don't give any description of the content of your stylesheet, but for example, where you now probably have something along the lines of:

  <xsl:apply-templates select="Practica"/>
  ...
  <xsl:template match="Practica">...

You will have to change this to:

  xmlns:ppztl="http://www.saga.it/egov/esicra/importazione/xml/esicra_pra"
  ...
  <xsl:apply-templates select="ppztl:Practica"/>
  ...
  <xsl:template match="ppztl:Practica">...

I hope this helps.

........................ Ken

--
Upcoming hands-on courses: in-house corporate training available;
North America public:                     XSLT/XPath Aug 12, 2003

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                      Definitive XSLT and XPath
ISBN 0-13-140374-5                              Definitive XSL-FO
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-11-X              Practical Formatting Using XSL-FO
Member of the XML Guild of Practitioners:    http://XMLGuild.info
Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc


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




Current Thread
Keywords