dynamic database processing

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

    dynamic database processing

    Hi,

    I have a xml-database with a changing amount of elements <A>. How do I write
    a XSLT-Script to find out how many elements <A> are inside the database in
    order to write them to an output file ?
    example:

    <A> P0 </A>
    <A> P1 </A>
    ....
    <A> Pn </A>

    Greets
    Thomas


  • Jürgen Kahrs

    #2
    Re: dynamic database processing

    Thomas Mann wrote:
    [color=blue]
    > I have a xml-database with a changing amount of elements <A>. How do I write
    > a XSLT-Script to find out how many elements <A> are inside the database in
    > order to write them to an output file ?[/color]

    This problem is so simple that you don't need
    XSLT for it. Here is a script in xmlgawk (tested):

    BEGIN { XMLMODE=1 }

    XMLSTARTELEM == "A" { inA=1; count ++ }
    XMLENDELEM == "A" { inA=0 }

    XMLCHARDATA && inA { print "element:", $0 }

    END { print "There are", count, "elements A" }

    The script counts elements "A" and prints each of
    them to standard output.
    [color=blue]
    > example:
    >
    > <A> P0 </A>
    > <A> P1 </A>
    > ...
    > <A> Pn </A>[/color]

    The result for this example is:

    element: P0
    element: P1
    element: Pn
    There are 3 elements A

    Comment

    • Thomas Mann

      #3
      Re: dynamic database processing

      Unfortunately I have to use XSLT.



      "Jürgen Kahrs" <Juergen.KahrsD ELETETHIS@vr-web.de> schrieb im Newsbeitrag
      news:2q0f7rFpor j3U1@uni-berlin.de...[color=blue]
      > Thomas Mann wrote:
      >[color=green]
      > > I have a xml-database with a changing amount of elements <A>. How do I[/color][/color]
      write[color=blue][color=green]
      > > a XSLT-Script to find out how many elements <A> are inside the database[/color][/color]
      in[color=blue][color=green]
      > > order to write them to an output file ?[/color]
      >
      > This problem is so simple that you don't need
      > XSLT for it. Here is a script in xmlgawk (tested):
      >
      > BEGIN { XMLMODE=1 }
      >
      > XMLSTARTELEM == "A" { inA=1; count ++ }
      > XMLENDELEM == "A" { inA=0 }
      >
      > XMLCHARDATA && inA { print "element:", $0 }
      >
      > END { print "There are", count, "elements A" }
      >
      > The script counts elements "A" and prints each of
      > them to standard output.
      >[color=green]
      > > example:
      > >
      > > <A> P0 </A>
      > > <A> P1 </A>
      > > ...
      > > <A> Pn </A>[/color]
      >
      > The result for this example is:
      >
      > element: P0
      > element: P1
      > element: Pn
      > There are 3 elements A[/color]


      Comment

      • William Park

        #4
        Re: dynamic database processing

        Thomas Mann <webriderabc@gm x.de> wrote:[color=blue]
        > Hi,
        >
        > I have a xml-database with a changing amount of elements <A>. How do I write
        > a XSLT-Script to find out how many elements <A> are inside the database in
        > order to write them to an output file ?
        > example:
        >
        > <A> P0 </A>
        > <A> P1 </A>
        > ...
        > <A> Pn </A>[/color]

        Assuming this is not a homework, you can try Bash shell with patch for
        Expat XML parser. For illustration,

        - Counting all elements:

        start() { # Usage: start tag att=value ...
        echo $((count++))
        }
        xml -s start '... <A>...</A>...'
        echo $count

        - Counting only <A> element:

        start() { # Usage: start tag att=value ...
        [ "$1" = A ] && echo $((count++))
        }
        xml -s start '... <A>...</A>...'
        echo $count

        Ref:
        Compare the best free open source Software Development Software at SourceForge. Free, secure and fast Software Development Software downloads from the largest Open Source applications and software directory


        help xml

        Library:
        You need Expat XML parser (www.libexpat.org). Most modern Linux
        distribution already has it, eg. Slackware.

        --
        William Park <opengeometry@y ahoo.ca>
        Open Geometry Consulting, Toronto, Canada

        Comment

        • William Park

          #5
          Re: dynamic database processing

          William Park <opengeometry@y ahoo.ca> wrote:[color=blue]
          > Thomas Mann <webriderabc@gm x.de> wrote:[color=green]
          > > Hi,
          > >
          > > I have a xml-database with a changing amount of elements <A>. How do I write
          > > a XSLT-Script to find out how many elements <A> are inside the database in
          > > order to write them to an output file ?
          > > example:
          > >
          > > <A> P0 </A>
          > > <A> P1 </A>
          > > ...
          > > <A> Pn </A>[/color]
          >
          > Assuming this is not a homework, you can try Bash shell with patch for
          > Expat XML parser. For illustration,
          >
          > - Counting all elements:
          >
          > start() { # Usage: start tag att=value ...
          > echo $((count++))
          > }
          > xml -s start '... <A>...</A>...'
          > echo $count
          >
          > - Counting only <A> element:
          >
          > start() { # Usage: start tag att=value ...
          > [ "$1" = A ] && echo $((count++))
          > }
          > xml -s start '... <A>...</A>...'
          > echo $count
          >
          > Ref:
          > http://freshmeat.net/projects/bashdiff/
          > http://home.eol.ca/~parkw/index.html#xml
          > help xml
          >
          > Library:
          > You need Expat XML parser (www.libexpat.org). Most modern Linux
          > distribution already has it, eg. Slackware.[/color]

          If you can't get Expat, then you'll have to resort to regex. Assuming
          <A>...</A> is non-nesting, that is '...' should not contain another
          <A> element,

          a=()
          array -p '<A>' -q '</A>' -V : a '... <A>...</A> ...'

          will give you array 'a' with all the <A> elements, both tag and content.
          If you want just the contents,

          func () { echo $2; }
          a=()
          array -p '<A>' -q '</A>' -E func -V : a '... <A>...</A> ...'

          --
          William Park <opengeometry@y ahoo.ca>
          Open Geometry Consulting, Toronto, Canada

          Comment

          • Patrick TJ McPhee

            #6
            Re: dynamic database processing

            In article <cheqn7$8eg$05$ 1@news.t-online.com>,
            Thomas Mann <webriderabc@gm x.de> wrote:

            % I have a xml-database with a changing amount of elements <A>. How do I write
            % a XSLT-Script to find out how many elements <A> are inside the database in
            % order to write them to an output file ?

            I'm assuming you don't really care how many elements you have, and that
            you just want to write them to an output file. When giving an example
            of this sort of problem, it's useful to provide both the expected input
            and the desired output. Your example provides little information. Anyway,
            you can do something like this:

            <xsl:stylshee t xmlns:xsl = 'http://www.w3.org/1999/XSL/Transform'
            version='1.0'>

            <!-- this template is here purely to generate well-formed output -->
            <xsl:template match='/'>
            <As>
            <xsl:apply-templates/>
            </As>
            </xsl:template>

            <xsl:template match='A'>
            <xsl:copy-of select = '.'/>
            </xsl:template>

            </xsl:stylesheet>

            --

            Patrick TJ McPhee
            East York Canada
            ptjm@interlog.c om

            Comment

            Working...