Re: [xsl] Adding prefix to elements that belong to certain namespace (correction)

Subject: Re: [xsl] Adding prefix to elements that belong to certain namespace (correction)
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 23 Sep 2003 19:57:36 -0400
At 2003-09-23 16:13 -0700, Venkat Gyambavantha wrote:
Corrected so that my result is well-formed

<envelope xmlns="http://somenamespace.com";> <!-- prefix this element with
some prefix -->
   <Header xmlns=""> <!-- remove xmlns attribute for this element -->
      <from>me</from>
      <to>you</to>
 </Header>
 <body xmlns="">
    <command xmlns="http://somenamespace.com";> <!-- prefix this element with
the prefix used for envelope -->
      <type xmlns="">ADD</type>
  </command>
</body>
</envelope>

to

<pfx:envelope xmlns:pfx="http://somenamespace.com";>
   <Header>
      <from>me</from>
      <to>you</to>
 </Header>
 <body>
    <pfx:command>
      <type>ADD</type>
  </pfx:command>
</body>
</pfx:envelope>

The information sets of the above two instances are the same ... there is *no* transformation going on above. Prefixes are irrelevant to XML processing.


Note, however, that processors are *not* obliged to preserve any prefix you put in a QName in an xsl:element instruction ... though most do and an example below using Saxon does. Ref. Section 7.1.2: "XSLT processors may make use of the prefix of the QName specified in the name attribute when selecting the prefix used for outputting the created element as XML; however, they are not required to do so."

So, you are asking for something that isn't guaranteed in XSLT for a reason that isn't necessary to XML giving a result that isn't different from the original... but a candidate answer is below since you asked.

I hope this helps.

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

T:\venkat>type venkat.xml
<envelope xmlns="http://somenamespace.com";> <!-- prefix this element with
some prefix -->
   <Header xmlns=""> <!-- remove xmlns attribute for this element -->
      <from>me</from>
      <to>you</to>
 </Header>
 <body xmlns="">
    <command xmlns="http://somenamespace.com";> <!-- prefix this element with
the prefix used for envelope -->
      <type xmlns="">ADD</type>
  </command>
</body>
</envelope>

T:\venkat>type venkat.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:pfx="http://somenamespace.com";
                version="1.0">

<xsl:template match="*">
  <xsl:element name="{local-name(.)}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="pfx:*">
  <xsl:element name="pfx:{local-name(.)}" namespace="{namespace-uri(.)}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="@*|text()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

T:\venkat>saxon venkat.xml venkat.xsl
<?xml version="1.0" encoding="utf-8"?><pfx:envelope xmlns:pfx="http://somenamespace.com";>
<Header>
<from>me</from>
<to>you</to>
</Header>
<body>
<pfx:command>
<type>ADD</type>
</pfx:command>
</body>
</pfx:envelope>
T:\venkat>




--
Next public US delivery:        3-day XSLT/2-day XSL-FO 2003-10-13
Next public European delivery:  3-day XSLT/2-day XSL-FO 2003-11-??
Instructor-led on-site corporate, government & user group training
for XSLT and XSL-FO world-wide:  please contact us for the details

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