dynamic database processing

T

Thomas Mann

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
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

Thomas said:
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
 
T

Thomas Mann

Unfortunately I have to use XSLT.



Jürgen Kahrs said:
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.


The result for this example is:

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

William Park

Thomas Mann said:
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.
 
W

William Park

William Park said:
Thomas Mann said:
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> ...'
 
P

Patrick TJ McPhee

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

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

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,830
Latest member
HeleneMull

Latest Threads

Top