dynamic database processing

Discussion in 'XML' started by Thomas Mann, Sep 5, 2004.

  1. Thomas Mann

    Thomas Mann Guest

    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
     
    Thomas Mann, Sep 5, 2004
    #1
    1. Advertising

  2. Thomas Mann 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 ?


    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.

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


    The result for this example is:

    element: P0
    element: P1
    element: Pn
    There are 3 elements A
     
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Sep 5, 2004
    #2
    1. Advertising

  3. Thomas Mann

    Thomas Mann Guest

    Unfortunately I have to use XSLT.



    "J├╝rgen Kahrs" <> schrieb im Newsbeitrag
    news:...
    > Thomas Mann 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 ?

    >
    > 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.
    >
    > > example:
    > >
    > > <A> P0 </A>
    > > <A> P1 </A>
    > > ...
    > > <A> Pn </A>

    >
    > The result for this example is:
    >
    > element: P0
    > element: P1
    > element: Pn
    > There are 3 elements A
     
    Thomas Mann, Sep 5, 2004
    #3
  4. Thomas Mann

    William Park Guest

    Thomas Mann <> wrote:
    > 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>


    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.

    --
    William Park <>
    Open Geometry Consulting, Toronto, Canada
     
    William Park, Sep 5, 2004
    #4
  5. Thomas Mann

    William Park Guest

    William Park <> wrote:
    > Thomas Mann <> wrote:
    > > 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>

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


    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 <>
    Open Geometry Consulting, Toronto, Canada
     
    William Park, Sep 5, 2004
    #5
  6. In article <cheqn7$8eg$05$-online.com>,
    Thomas Mann <> 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:stylsheet 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
     
    Patrick TJ McPhee, Sep 6, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Williamson
    Replies:
    2
    Views:
    462
    Rocky Moore
    Aug 15, 2004
  2. Luis Esteban Valencia
    Replies:
    1
    Views:
    1,415
    Carl Prothman [MVP]
    Jan 12, 2005
  3. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    531
    Michael Foord
    Sep 17, 2004
  4. Peter Laman

    Dynamic form processing

    Peter Laman, May 6, 2008, in forum: Javascript
    Replies:
    4
    Views:
    102
    Peter Laman
    May 8, 2008
  5. icogs

    Dynamic XML document processing.

    icogs, Mar 3, 2010, in forum: Javascript
    Replies:
    2
    Views:
    108
Loading...

Share This Page