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 February 20th, 2006, 09:25 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Namespace copy problem

I have an XML document similar to this:

<?xml version="1.0" ?>
<Document xmlns="http://mydomain.com/DocumentSchema.xsd">
    <Submission>
        <AddressDetails>
            <Flat />
            <HouseName />
            <HouseNumber>1</HouseNumber>
            <Street>Test Street</Street>
            <District>Test District</District>
            <Town>Test Town</Town>
            <County>test County</County>
            <Postcode>TEST123</Postcode>
        </AddressDetails>
        <BankDetails>
            <SortCode/>
            <AccountNumber/>
        </BankDetails>
    </Submission>
</Document>

I have got a problem with some XSL. Basically I want my output to select all the nodes including <AddressDetails> to give:

<?xml version="1.0" ?>
<AddressDetails>
    <Flat />
    <HouseName />
    <HouseNumber>1</HouseNumber>
    <Street>Test Street</Street>
    <District>Test District</District>
    <Town>Test Town</Town>
    <County>test County</County>
    <Postcode>TEST123</Postcode>
</AddressDetails>

I think it is the namespace that is causing the issue.

My XSL is as follows:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:test="http://mydomain.com/DocumentSchema.xsd" exclude-result-prefixes="test">
  <xsl:output method="xml" indent="no"/>
  <xsl:template match="test:Submission">
        <xsl:element name="AddressDetails">
            <xsl:copy-of select="*" />
            <xsl:apply-templates mode="copy-no-ns" select="node()" />
        </xsl:element>
    </xsl:template>
    <xsl:template mode="copy-no-ns" match="*">
        <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
            <xsl:copy-of select="@*" />
            <xsl:apply-templates mode="copy-no-ns" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

The result includes BankDetails as well.

If I change the xsl:template match to "test:AddressDetails" it errors with "The data at the root level is invalid."

Any help would be most appreciated.



 
Old February 20th, 2006, 11:16 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Just add a template rule that throws away the BankDetails:

<xsl:template mode="copy-no-ns" match="test:BankDetails"/>

Also, this is quite wrong:

  <xsl:template match="test:Submission">
        <xsl:element name="AddressDetails">
            <xsl:copy-of select="*" />
            <xsl:apply-templates mode="copy-no-ns" select="node()" />
        </xsl:element>
    </xsl:template>

The xsl:copy-of select="*" copies all children of the Submission, which you don't want. I would get rid of this rule and replace it with

<xsl:template match="/">
  <xsl:apply-templates select="//test:AddressDetails"/>
</xsl:template>

The error you report looks like a red herring.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 20th, 2006, 11:46 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Mike. That's pretty much it except I'm only getting the data from the XML elements. The nodes appear to be missing.

The following XSL:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:test="http://mydomain.com/DocumentSchema.xsd" exclude-result-prefixes="test">
    <xsl:output method="xml" indent="no" />
    <xsl:template match="/">
        <xsl:element name="AddressDetails">
            <xsl:apply-templates select="//test:AddressDetails/node()" />
        </xsl:element>
    </xsl:template>
    <xsl:template mode="copy-no-ns" match="*">
        <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
            <xsl:copy-of select="@*" />
            <xsl:apply-templates mode="copy-no-ns" />
        </xsl:element>
    </xsl:template>
    <xsl:template mode="copy-no-ns" match="test:BankDetails" />
</xsl:stylesheet>

Gives:

<?xml version="1.0" ?>
<AddressDetails>1Test StreetTest DistrictTest Towntest CountyTEST123</AddressDetails>

Is it possible to get the elements as well?


 
Old February 20th, 2006, 01:07 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your terminology is a bit off: an element is a kind of node.

You need to specify mode="copy-no-ns" on the apply-templates call (or remove it from the template rules).

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
Changing namespace on copy rushman XSLT 2 November 12th, 2007 02:35 PM
Namespace problem adwaitjoshi ASP.NET 2.0 Professional 0 March 28th, 2007 10:04 PM
namespace problem gantait XSLT 5 February 19th, 2007 12:51 AM
Namespace Problem JRMotz BOOK: ASP.NET Website Programming Problem-Design-Solution 1 November 12th, 2005 03:18 PM
copy-of xsi namespace chris_strub XSLT 2 October 2nd, 2004 10:32 PM





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