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 May 1st, 2007, 06:41 AM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Default and blank namespaces

I have a source XML document to transform (from a 3rd party) like this

<SOSA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.nonesuch.com/ns/namespace/v2">
    <Header xmlns="">
        <Version>2.0</Version>
        <Mode>TEST</Mode>
    </Header>
    <Authentication xmlns="">
        <Username/>
        <Password/>
    </Authentication>
    <SOR xmlns="">
        <Reference>E20150645</Reference>
        <AllocatedBy>001</AllocatedBy>
    </SOR>
    <SOR xmlns="">
        <Reference>86072||25671</Reference>
        <AllocatedBy>002</AllocatedBy>
    </SOR>
    <SOR xmlns="">
        <Reference>79376</Reference>
        <AllocatedBy>003</AllocatedBy>
    </SOR>
</SOSA>

As you can see it has a default namespace but also has xmlns="" declared in various elements. I think I know why the document is like this but I can't work out how my stylesheet should be written.

My xsl looks like this at the moment but this doesn't find SOR.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xpath-default-namespace="http://www.nonesuch.com/ns/namespace/v2">
    <xsl:template match="/SOSA">
        <Data>
            <Header>
                <SenderID>xxx</SenderID>
                <xsl:for-each select="SOR">
                    <xsl:if test="AllocatedBy='001'">
                        <TargetID>
                            <xsl:value-of select="Reference"/>
                        </TargetID>
                    </xsl:if>
                </xsl:for-each>
            </Header>
        </Data>
    </xsl:template>
</xsl:stylesheet>

How do I tell the XSL to use xmlns="" for these elements?

Hope this makes sense and TIA


 
Old May 1st, 2007, 07:34 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

On the whole I'd suggest that if some of your source elements are in a namespace and others aren't, then it's better to bind an explicit prefix in the stylesheet and use it whenever you refer to an element in a namespace, for example <xsl:template match="/p1:SOSA">. That's the only way to do it in XSLT 1.0.

However, if you want to use xpath-default-namespace you can, for example you can write

 <xsl:for-each select="SOR" xpath-default-namespace="">

which will override the value used at the xsl:stylesheet level.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 1st, 2007, 09:56 AM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help - took the <xsl:for-each select="SOR" xpath-default-namespace=""> route.

Thanks again

Stewart






Similar Threads
Thread Thread Starter Forum Replies Last Post
Blank Default Page ghrebek BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 8 December 5th, 2006 08:09 PM
Namespaces Amateur BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 November 27th, 2006 05:19 PM
namespaces anchal C# 1 July 3rd, 2006 02:53 PM
Problems with namespaces (I think) hasanali00 BOOK: ASP.NET Website Programming Problem-Design-Solution 3 November 19th, 2005 02:05 AM
NAMESPACES - WHAT ARE THEY? p_nut33 C# 2 July 31st, 2003 03:18 AM





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