Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 17th, 2008, 08:51 PM
Registered User
 
Join Date: Jan 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extended namespace not showing in xml

Here is a xslt that i have where I have two namespaces. When I transform an existing xml using this xslt, the output does not list both the namespaces at the top. It does not list the "xmlns:test" namespace at the top. Instead it lists the namespace for each extended element. for e.g.
<test:RecievingType xmlns:test="http://test/retail/ArtsPOSLogExt/">XGRF</test:RecievingType>

Do you know how I can fix this?


XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:test="http://test/retail/ArtsPOSLogExt/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns="http://www.nrf-arts.org/IXRetail/namespace/" xsi:schemaLocation="http://test/retail/ArtsPOSLogExt/ ArtsPOSLogExtForReceipt.xsd http://www.nrf-arts.org/IXRetail/namespace/ POSLogV221.xsd">
  <xsl:template match="/">
    <xsl:element name="POSLog">
      <xsl:attribute name="xsi:schemaLocation" >http://test/retail/ArtsPOSLogExt/ ArtsPOSLogExtForReceipt.xsd http://www.nrf-arts.org/IXRetail/namespace/ POSLogV221.xsd</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="/results/st">
    <xsl:element name="Transaction">
      <xsl:element name="RetailStoreID">
        <xsl:value-of select="@RetailStoreID"/>
      </xsl:element>
      <xsl:element name="test:RecievingType">
        <xsl:value-of select="@ReceivingType"/>
      </xsl:element>
       <xsl:element name="test:PackingSlip">
       <xsl:value-of select="substring(@Comments ,5)"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

XML

<?xml version="1.0" encoding="utf-8"?>
<POSLog xsi:schemaLocation="http://test/retail/ArtsPOSLogExt/ ArtsPOSLogExtForReceipt.xsd http://www.nrf-arts.org/IXRetail/namespace/ POSLogV221.xsd" xmlns="http://www.nrf-arts.org/IXRetail/namespace/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Transaction>
    <RetailStoreID>3</RetailStoreID>
    <test:RecievingType xmlns:test="http://test/retail/ArtsPOSLogExt/">XGRF</test:RecievingType>
   <test:PackingSlip xmlns:test="http://test/retail/ArtsPOSLogExt/"></test:PackingSlip>
  </Transaction>
</POSLog>


 
Old January 18th, 2008, 05:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The simplest solution is to use literal result elements instead of xsl:element in all cases where the element name is a constant. That is, change

  <xsl:template match="/">
    <xsl:element name="POSLog">
      <xsl:attribute name="xsi:schemaLocation" >http://test/retail/ArtsPOSLogExt/ ArtsPOSLogExtForReceipt.xsd http://www.nrf-arts.org/IXRetail/namespace/ POSLogV221.xsd</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

to

  <xsl:template match="/">
    <POSLog xsi:schemaLocation="http://test/retail/ArtsPOSLogExt/
             ArtsPOSLogExtForReceipt.xsd
           http://www.nrf-arts.org/IXRetail/namespace/
             POSLogV221.xsd">
      <xsl:apply-templates/>
    </PosLog>
  </xsl:template>

Firstly, literal result elements are a lot more readable. Secondly, a literal result element copies all the namespaces that are in scope in the stylesheet, except any that you exclude using exclude-result-prefixes. Generally this gives a better result than xsl:element, which doesn't copy namespaces from the stylesheet.

The alternative, since you're using XSLT 2.0, is to use the xsl:namespace instruction to explicitly attach a namespace node to the POSLog element.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Transforming XML From One Namespace to Another mail4kaja XSLT 1 November 10th, 2008 10:14 AM
Adding Prefix and Namespace to XML Lerxt XSLT 6 November 8th, 2007 03:14 PM
convert XML to XMl - Another Namespace question bonekrusher XSLT 2 July 10th, 2007 07:32 AM
Analysis for getting XML and showing in client karveajit C# 0 August 3rd, 2006 01:36 AM
xml namespace and asp werD420 XML 1 August 12th, 2005 01:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.