XML to text output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim

    #1

    XML to text output

    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:styleshe et version="1.0"
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

    <xsl:output 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!

  • Martin Honnen

    #2
    Re: XML to text output



    Jim wrote:

    [color=blue]
    > 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>[/color]

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

    [color=blue]
    > 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?[/color]

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

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Jim

      #3
      Re: XML to text output

      Martin Honnen wrote:[color=blue]
      >
      >
      > Jim wrote:
      >
      >[color=green]
      >> 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>[/color]
      >
      >
      > the above is not well-formed as it doesn't have a single root element
      > that contains all other elements.
      >
      >[color=green]
      >> 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?[/color]
      >
      >
      > You need to correct your XML to have a root element.
      >[/color]

      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.

      Comment

      • Philippe

        #4
        Re: XML to text output

        On Mon, 17 May 2004 11:44:16 -0400, Jim <no@no.no> wrote:
        [color=blue]
        >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>[/color]

        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>
        </areas>
        [color=blue]
        >
        >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:styleshee t version="1.0"
        >xmlns:xsl="htt p://www.w3.org/1999/XSL/Transform">
        >
        ><xsl:output method="text"/>
        >
        ><xsl:templat e match="area1">
        > <xsl:value-of select="name"/>
        > <xsl:value-of select="age"/>
        ></xsl:template>
        >
        ><xsl:templat e 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![/color]

        --
        Philippe

        Comment

        • Martin Honnen

          #5
          Re: XML to text output



          Jim wrote:

          [color=blue][color=green]
          >> You need to correct your XML to have a root element.
          >>[/color]
          >
          > 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.[/color]

          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.
          --

          Martin Honnen
          http://JavaScript.FAQTs.com/

          Comment

          • Jim

            #6
            Re: XML to text output

            Martin Honnen wrote:
            [color=blue]
            >
            >
            > Jim wrote:
            >
            >[color=green]
            >> 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>[/color]
            >
            >
            > the above is not well-formed as it doesn't have a single root element
            > that contains all other elements.
            >
            >[color=green]
            >> 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?[/color]
            >
            >
            > You need to correct your XML to have a root element.
            >[/color]

            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>


            Comment

            • Jim

              #7
              Re: XML to text output

              Jim wrote:
              [color=blue]
              > Martin Honnen wrote:
              >[color=green]
              >>
              >>
              >> Jim wrote:
              >>
              >>[color=darkred]
              >>> 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>[/color]
              >>
              >>
              >>
              >> the above is not well-formed as it doesn't have a single root element
              >> that contains all other elements.
              >>
              >>[color=darkred]
              >>> 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?[/color]
              >>
              >>
              >>
              >> You need to correct your XML to have a root element.
              >>[/color]
              >
              > 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>
              >
              >[/color]

              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.

              Comment

              • Alex Shirshov

                #8
                Re: XML to text output

                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.


                Comment

                • Jim

                  #9
                  Re: XML to text output

                  Alex Shirshov wrote:
                  [color=blue]
                  > 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.
                  >
                  >[/color]

                  Thanks Alex. Just what I needed.

                  Jim

                  Comment

                  Working...