XML to text output

J

Jim

Hi

I'm fumbling through xml and I'm having a problem which I'm sure is easy
to correct. Here's the xml:

<area1>
<name>John</name>
<age>22</age>
</area1>
<area2>
<city>London</city>
<state>OH</state>
</area2>

I want to pull out name/age/city/state. Seems simple, but because I
have another element (is that the correct term?) area2, things get all
munged up. Like I said, this is very simple. Here's my xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="text"/>

<xsl:template match="area1">
<xsl:value-of select="name"/>
<xsl:value-of select="age"/>
</xsl:template>

<xsl:template match="area2">
<xsl:value-of select="city"/>
<xsl:value-of select="state"/>
</xsl:template>

When I try and apply this I get an error:

% xsltproc test.xml test.xsl
test.xml:6: parser error : Extra content at the end of the document
<area2>
^
cannot parse test.xml

What am I doing wrong?

Thanks!
 
M

Martin Honnen

Jim wrote:

I'm fumbling through xml and I'm having a problem which I'm sure is easy
to correct. Here's the xml:

<area1>
<name>John</name>
<age>22</age>
</area1>
<area2>
<city>London</city>
<state>OH</state>
</area2>

the above is not well-formed as it doesn't have a single root element
that contains all other elements.

When I try and apply this I get an error:

% xsltproc test.xml test.xsl
test.xml:6: parser error : Extra content at the end of the document
<area2>
^
cannot parse test.xml

What am I doing wrong?

You need to correct your XML to have a root element.
 
J

Jim

Martin said:
Jim wrote:




the above is not well-formed as it doesn't have a single root element
that contains all other elements.




You need to correct your XML to have a root element.

So you can't have multiple root elements in the same XML? Crap--I can't
change the XML--it's what I've been given to work with.
 
P

Philippe

Hi

I'm fumbling through xml and I'm having a problem which I'm sure is easy
to correct. Here's the xml:

<area1>
<name>John</name>
<age>22</age>
</area1>
<area2>
<city>London</city>
<state>OH</state>
</area2>

Your XML File must have only one root :
<areas>
<area1>
<name>John</name>
<age>22</age>
</area1>
<area2>
<city>London</city>
<state>OH</state>
</area2>
 
M

Martin Honnen

Jim wrote:

So you can't have multiple root elements in the same XML? Crap--I can't
change the XML--it's what I've been given to work with.

Check the XML specification
http://www.w3.org/TR/REC-xml/#sec-documents
it says
A data object is an XML document if it is well-formed, as defined in
this specification.
and the production rule for document is
document ::= prolog element Misc*
so in an XML document there is exactly one root element.

What you have been given is formally no XML.
 
J

Jim

Martin said:
Jim wrote:




the above is not well-formed as it doesn't have a single root element
that contains all other elements.




You need to correct your XML to have a root element.

Ok, what if I wrapped the whole thing:

<file>
<area1>
<name>John</name>
<age>22</age>
</area1>
<area2>
<city>London</city>
<state>OH</state>
</area2>
</file>
 
J

Jim

Jim said:
Ok, what if I wrapped the whole thing:

<file>
<area1>
<name>John</name>
<age>22</age>
</area1>
<area2>
<city>London</city>
<state>OH</state>
</area2>
</file>

I figured it out. I can wrap the whole code in tags and then apply my
sheet.

How can I ignore elements? Say I have the xml:

<file>
<area1>
<name>John</name>
<age>22</age>
</area1>
<area2>
<city>London</city>
<state>OH</state>
</area2>
</file>

and I only want to output some of the elements. Maybe all of area1, but
none of area2. Or all of area1, but just area2/city.

Thanks for all the assistance.
 
A

Alex Shirshov

Hello, Jim!
You wrote on Mon, 17 May 2004 14:01:54 -0400:


[Sorry, skipped]

J> How can I ignore elements? Say I have the xml:

J> <file>
J> <area1>
J> <name>John</name>
J> <age>22</age>
J> </area1>
J> <area2>
J> <city>London</city>
J> <state>OH</state>
J> </area2>
J> </file>

J> and I only want to output some of the elements.

<xsl:copy-of/>

J> Maybe all of area1, but none of area2.

[xslt]
<xsl:template match="area1">
<xsl:copy-of select="."/>
</xsl:template match>

<xsl:template match="area2"/>
[/xslt]

J> Or all of area1, but just area2/city.

[xslt]
<xsl:template match="area1">
<xsl:copy-of select="."/>
</xsl:template match>

<xsl:template match="area2">
<xsl:copy-of select="city"/>
</xsl:template match>
[/xslt]

With best regards, Alex Shirshov.
 
J

Jim

Alex said:
Hello, Jim!
You wrote on Mon, 17 May 2004 14:01:54 -0400:


[Sorry, skipped]

J> How can I ignore elements? Say I have the xml:

J> <file>
J> <area1>
J> <name>John</name>
J> <age>22</age>
J> </area1>
J> <area2>
J> <city>London</city>
J> <state>OH</state>
J> </area2>
J> </file>

J> and I only want to output some of the elements.

<xsl:copy-of/>

J> Maybe all of area1, but none of area2.

[xslt]
<xsl:template match="area1">
<xsl:copy-of select="."/>
</xsl:template match>

<xsl:template match="area2"/>
[/xslt]

J> Or all of area1, but just area2/city.

[xslt]
<xsl:template match="area1">
<xsl:copy-of select="."/>
</xsl:template match>

<xsl:template match="area2">
<xsl:copy-of select="city"/>
</xsl:template match>
[/xslt]

With best regards, Alex Shirshov.

Thanks Alex. Just what I needed.

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top