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 April 16th, 2012, 02:52 PM
Authorized User
 
Join Date: Jan 2012
Posts: 17
Thanks: 7
Thanked 0 Times in 0 Posts
Default Namespace problems in an identity transform

Hi all --

Many thanks for reading. I have a problem with running an identity transform on XML documents with namespaces. I can pre-process outside of XSLT and avoid the issue, but I'd like to be able to do this entirely inside XSLT 2.0.

Note: to simplify the examples, I've created XML documents that aren't schema-conformant. Avoid testing the transform in a schema-aware processor or you'll get errors!

Sample input (also available here: http://pastebin.com/RFAQaY3w)

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ead xsi:schemaLocation="urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd" xmlns:ns2="http://www.w3.org/1999/xlink" xmlns="urn:isbn:1-931666-22-9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <eadheader findaidstatus="Ready_for_online" repositoryencoding="iso15511" countryencoding="iso3166-1" dateencoding="iso8601" langencoding="iso639-2b">	
	<eadid>01234</eadid>
	<filedesc>
		<AAA>1</AAA>
		<BBB>2</BBB>
	</filedesc>
	</eadheader>
	<archdesc>
		<bib>
			<CCC>1</CCC>
			<DDD>2</DDD>
		</bib>
	</archdesc>
</ead>
Here is the XSLT:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0"
    xsi:schemaLocation="urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd"
    xmlns:ns2="http://www.w3.org/1999/xlink" 
    xmlns="urn:isbn:1-931666-22-9"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    <xsl:output method="xml" indent="yes" standalone="no"/>
    
    <!-- The identity transform -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="filedesc">
        <xsl:copy>
            <header>Subjects</header>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
Desired output:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ead xsi:schemaLocation="urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd" xmlns:ns2="http://www.w3.org/1999/xlink" xmlns="urn:isbn:1-931666-22-9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<eadheader findaidstatus="Ready_for_online" repositoryencoding="iso15511" countryencoding="iso3166-1" dateencoding="iso8601" langencoding="iso639-2b">  
    <eadid>01234</eadid>
    <filedesc>
        <header>Subjects</header>
        <AAA>1</AAA>
        <BBB>2</BBB>
    </filedesc>
</eadheader>
<archdesc>
    <bib>
        <CCC>1</CCC>
        <DDD>2</DDD>
    </bib>
</archdesc>
</ead>
Again, if I strip out all of the <ead> namespace values, the transform works. With the namespace values in the XSL, the transform reproduces an exact copy without the new <header> element.

Any advice is greatly appreciated. Thanks again for reading and your suggestions!
 
Old April 16th, 2012, 04:25 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

match="filedesc" matches an element whose local name is filedesc in no namespace. To match a filedesc element in namespace urn:isbn:1-931666-22-9", you need

Code:
<xsl:template match="u:filedesc" xmlns:u="urn:isbn:1-931666-22-9">
This is the most commonly asked question about XSLT by a very large margin.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 17th, 2012, 09:38 AM
Authorized User
 
Join Date: Jan 2012
Posts: 17
Thanks: 7
Thanked 0 Times in 0 Posts
Default

hi Michael --

thanks so much for the reply. I'm testing my stylesheets in the oXygen XML Editor, using Saxon-HE 9.3.0.5. The following error comes up:
Code:
Attribute @namespace is not allowed on element <xsl:template>
Running the transform with Saxon 9.1.0.8J gives the same error.

However, using
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0"
    xsi:schemaLocation="urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd"
    xmlns:ns2="http://www.w3.org/1999/xlink" 
    xmlns="urn:isbn:1-931666-22-9"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xpath-default-namespace="urn:isbn:1-931666-22-9">
attribute seems to make everything happy. Or, I could use the xpath-default-namespace in individual
Code:
<xsl:template select="filedesc" xpath-default-namespace="...">
. If I'm understanding this correctly, the xpath-default-namespace establishes a global namespace in the stylesheet. Is that correct? Namespaces are intense. :)

Thanks again for your assistance.
Cheers!





Similar Threads
Thread Thread Starter Forum Replies Last Post
identity transform excluding xml element ShaileshShinde XSLT 8 November 8th, 2011 08:04 AM
identity transform excluding xml element ShaileshShinde BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 1 November 2nd, 2011 09:22 AM
XSL sort on identity transform imshriram XSLT 1 October 14th, 2011 03:57 PM
Getting @@IDENTITY in Row Transform afward SQL Server DTS 2 September 28th, 2005 09:56 AM
Identity Insert Problems hortoristic SQL Server 2000 2 April 13th, 2004 12:03 PM





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