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 July 29th, 2010, 10:33 AM
Authorized User
 
Join Date: Jul 2010
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default Don't understand which errors

My source document:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<RootNode>
    <Name>Bob</Name>
    <ContactDetails>
        <Address>
            <Line1>1 High Street</Line1>
            <Town>TownName</Town>
            <Postcode>AB1 1CD</Postcode>
        </Address>
        <Email>[email protected]</Email>
    </ContactDetails>
    <AltContactDetails>
        <Address>
            <Line1>3 Market Square</Line1>
            <Town>TownName</Town>
            <Postcode>EF2 2GH</Postcode>
        </Address>
        <Email>[email protected]</Email>
    </AltContactDetails>
</RootNode>
My transformation document:
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
		<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="RootNode">
	  <PersonsName>
		<xsl:value-of select="Name"/>
	  </PersonsName>
	  <xsl:apply-templates select="ContactDetails|AltContactDetails" />
	</xsl:template>

<xsl:template match="ContactDetails|AltContactDetails">
  <xsl:copy>
    <FirstLine>
      <xsl:value-of select="Address/Line1"/>
    </FirstLine>
    <Town>
      <xsl:value-of select="Address/Town"/>
    </Town>
    <PostalCode>
      <xsl:value-of select="Address/Postcode"/>
    </PostalCode>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>
And the error message look like this:
Only one top level element is allowed in an XML document. Error processing resource

Anybody help me find out the errors?
Thanks,
 
Old July 29th, 2010, 10:38 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

That is an error message the XML parser gives, not the XSLT processor. So either your XML or your stylesheet is not a well-formed XML document with a single root element. Your posted samples seem fine however so somehow you are processing different documents when you get the error.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old July 29th, 2010, 10:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The error is in your output XML - which isn't a valid XML document.

Code:
<PersonName>
...
</PersonName>
<ContactDetails>
...
</ContactDetails>
<AltContactDetails>
...
</AltContactDetails>
You need to wrap the whole XML fragment in another XML element (e.g. like RootNode in your input XML) or put the contact details inside the PersonName.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
metinhoclam (July 30th, 2010)
 
Old July 29th, 2010, 10:59 AM
Authorized User
 
Join Date: Jul 2010
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default I still get confusing...

I knew the Name is the same level with ContactDetails, AltContactDetails. And I've already checked these documents a lot of time...I didnot see any errors....please give me your solution for my xslt...the source file was file...i think..I'm using Altova XML spy to run these files...
 
Old July 29th, 2010, 11:11 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You cannot have Name at the same level as ContactDetails without an outer XML element.

Code:
<RootNode>
  <PersonName>
  ...
  </PersonName>
  <ContactDetails>
  ...
  </ContactDetails>
  <AltContactDetails>
  ...
  </AltContactDetails>
</RootNode>
What you want to call this element is up to you.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 29th, 2010, 05:11 PM
Authorized User
 
Join Date: Jul 2010
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default problem with output

I have change the source file:
<?xml version="1.0" encoding="UTF-8"?>
<RootNode>
<Name>
BOB
<ContactDetails>
<Address>
<Line1>1 High Street</Line1>
<Town>TownName</Town>
<Postcode>AB1 1CD</Postcode>
</Address>
<Email>[email protected]</Email>
</ContactDetails>
<AltContactDetails>
<Address>
<Line1>3 Market Square</Line1>
<Town>TownName</Town>
<Postcode>EF2 2GH</Postcode>
</Address>
<Email>[email protected]</Email>
</AltContactDetails>
</Name>
</RootNode>
----
But I got the unexpected result...
--------------
<?xml version="1.0" encoding="UTF-8" ?>
<PersonsName xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">BOB 1 High StreetTownNameAB1 [email protected]3 Market SquareTownNameEF2 [email protected]</PersonsName>
-----
I usually encounter with the same problem with match template like this..how can i get the result with the tag...
Need your help! Thanks
-----
 
Old July 29th, 2010, 05:44 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Why is this result unexpected? You did <xsl:value-of select="Name"/>, which gives you the string value of the Name element, that is, all the text inside the Name element. What result did you expect, if not this? (And why did you change the source document to something so strange?)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old July 30th, 2010, 01:39 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

To fix your problem you don't need to change your input XML, but the output XML. To do this you need to add a root node to the output you are generating in your XSLT - something like this:

Code:
<xsl:template match="RootNode">
    <RootNode>
        <PersonsName>
            <xsl:value-of select="Name"/>
        </PersonsName>
        <xsl:apply-templates select="ContactDetails|AltContactDetails" />
    </RootNode>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
metinhoclam (July 30th, 2010)
 
Old July 30th, 2010, 04:54 AM
Authorized User
 
Join Date: Jul 2010
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default Thanks Mr.mhkay. I am a newbie...and I always get confusing with 'match'

I misunderstood about the xsl:apply-templates and select...I thought if select like that, then it just returns one value...
Thank,





Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me understand SeanDunn BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 3 November 14th, 2009 02:38 PM
I don't understand... jmsherry ASP.NET 2.0 Basics 17 July 23rd, 2007 12:28 PM
I still do not understand aude_poullain ASP.NET 1.0 and 1.1 Basics 2 February 7th, 2007 09:11 PM
Can't get errors to display with <html:errors> michaeldill JSP Basics 0 August 2nd, 2004 01:47 PM
i can't understand this error! ion Classic ASP Databases 2 September 23rd, 2003 03:57 PM





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