Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Check for existence of a tag

Reply
Thread Tools

Check for existence of a tag

 
 
Tassilo J. Klein
Guest
Posts: n/a
 
      08-20-2004
Hi there,

I got a XML file looking like that:

<Entries>
<Entry Id=1>
<First></First>
<Second></Second>
<Third></Third>
</Entry>
<Entry Id=2>
<First></First>
<Second></Second>
<Third></Third>
<Fourth></Fourth>
</Entry>.
[...]

<Entries>


I would like to check whether there exists a tag inside one of the
"Entry"s e.g. "Fourth".

I tried something like:

<xsl:template match="First">

<xsl:when test="..//Fourth">
<xsl:text>Found it</xsl:text>
</xsl:when>

<xsltherwise>
<xsl:text>Didn't find it</xsl:text>
</xsltherwise>

</xsl:template>

But it somehow doesn't work - I don't know why - probably, because I am
quite new to this . Any idea?

Greetings,
- Tassilo -
 
Reply With Quote
 
 
 
 
William Park
Guest
Posts: n/a
 
      08-20-2004
Tassilo J. Klein <> wrote:
> Hi there,
>
> I got a XML file looking like that:
>
> <Entries>
> <Entry Id=1>
> <First></First>
> <Second></Second>
> <Third></Third>
> </Entry>
> <Entry Id=2>
> <First></First>
> <Second></Second>
> <Third></Third>
> <Fourth></Fourth>
> </Entry>.
> [...]
>
> <Entries>
>
>
> I would like to check whether there exists a tag inside one of the
> "Entry"s e.g. "Fourth".


To test if 'Entry' is right above 'Fourth',

start () { # Usage: start tag att=value ...
if [[ $1 == Fourth ]]; then
n=$XML_ELEMENT_STACK
if [[ ${XML_ELEMENT_STACK[n+1+1]} == Entry ]]; then
echo found Entry/Fourth
fi
fi
}
xml -s start "<Entries>...</Entries>"

To test if 'Entry' is anywhere above 'Fourth',

start () { # Usage: start tag att=value ...
if [[ $1 == Fourth ]]; then
n=$XML_ELEMENT_STACK
if [[ ${XML_ELEMENT_STACK[*]|/Entry} }}; then
echo found Entry/.../Fourth
fi
fi
}
xml -s start "<Entries>...</Entries>"

Ref:
http://freshmeat.net/projects/bashdiff/
http://home.eol.ca/~parkw/index.html#xml
help xml

>
> I tried something like:
>
> <xsl:template match="First">
>
> <xsl:when test="..//Fourth">
> <xsl:text>Found it</xsl:text>
> </xsl:when>
>
> <xsltherwise>
> <xsl:text>Didn't find it</xsl:text>
> </xsltherwise>
>
> </xsl:template>
>
> But it somehow doesn't work - I don't know why - probably, because I am
> quite new to this . Any idea?
>
> Greetings,
> - Tassilo -


--
William Park <>
Open Geometry Consulting, Toronto, Canada
 
Reply With Quote
 
 
 
 
Tassilo J. Klein
Guest
Posts: n/a
 
      08-20-2004
Ooops, I forgot to mention, that I am using XLST to produce HTML and
therefore want to make those checks using XLS.

> Tassilo J. Klein <> wrote:
>
>>Hi there,
>>
>>I got a XML file looking like that:
>>
>><Entries>
>> <Entry Id=1>
>> <First></First>
>> <Second></Second>
>> <Third></Third>
>> </Entry>
>> <Entry Id=2>
>> <First></First>
>> <Second></Second>
>> <Third></Third>
>> <Fourth></Fourth>
>> </Entry>.
>> [...]
>>
>><Entries>
>>
>>
>>I would like to check whether there exists a tag inside one of the
>>"Entry"s e.g. "Fourth".

>
>
> To test if 'Entry' is right above 'Fourth',
>
> start () { # Usage: start tag att=value ...
> if [[ $1 == Fourth ]]; then
> n=$XML_ELEMENT_STACK
> if [[ ${XML_ELEMENT_STACK[n+1+1]} == Entry ]]; then
> echo found Entry/Fourth
> fi
> fi
> }
> xml -s start "<Entries>...</Entries>"
>
> To test if 'Entry' is anywhere above 'Fourth',
>
> start () { # Usage: start tag att=value ...
> if [[ $1 == Fourth ]]; then
> n=$XML_ELEMENT_STACK
> if [[ ${XML_ELEMENT_STACK[*]|/Entry} }}; then
> echo found Entry/.../Fourth
> fi
> fi
> }
> xml -s start "<Entries>...</Entries>"
>
> Ref:
> http://freshmeat.net/projects/bashdiff/
> http://home.eol.ca/~parkw/index.html#xml
> help xml
>
>
>>I tried something like:
>>
>><xsl:template match="First">
>>
>> <xsl:when test="..//Fourth">
>> <xsl:text>Found it</xsl:text>
>> </xsl:when>
>>
>> <xsltherwise>
>> <xsl:text>Didn't find it</xsl:text>
>> </xsltherwise>
>>
>></xsl:template>
>>
>>But it somehow doesn't work - I don't know why - probably, because I am
>>quite new to this . Any idea?
>>
>>Greetings,
>> - Tassilo -

>
>

 
Reply With Quote
 
kahrs
Guest
Posts: n/a
 
      08-20-2004
William Park wrote:

> To test if 'Entry' is anywhere above 'Fourth',
>
> start () { # Usage: start tag att=value ...
> if [[ $1 == Fourth ]]; then
> n=$XML_ELEMENT_STACK
> if [[ ${XML_ELEMENT_STACK[*]|/Entry} }}; then
> echo found Entry/.../Fourth
> fi
> fi
> }
> xml -s start "<Entries>...</Entries>"


Your mechanism for stacking the elements is really useful.
Quite interesting to see how you integrated this into bash.
But (as you might have guessed) I prefer the xmlgawk solution:


XMLSTARTELEM == "Fourth" { found=1 }

END {
if (found)
print "Found it"
else
print "Didn't find it"
}
 
Reply With Quote
 
William Park
Guest
Posts: n/a
 
      08-21-2004
kahrs <> wrote:
> William Park wrote:
>
> > To test if 'Entry' is anywhere above 'Fourth',
> >
> > start () { # Usage: start tag att=value ...
> > if [[ $1 == Fourth ]]; then
> > n=$XML_ELEMENT_STACK
> > if [[ ${XML_ELEMENT_STACK[*]|/Entry} }}; then
> > echo found Entry/.../Fourth
> > fi
> > fi
> > }
> > xml -s start "<Entries>...</Entries>"

>
> Your mechanism for stacking the elements is really useful.
> Quite interesting to see how you integrated this into bash.
> But (as you might have guessed) I prefer the xmlgawk solution:
>
>
> XMLSTARTELEM == "Fourth" { found=1 }
>
> END {
> if (found)
> print "Found it"
> else
> print "Didn't find it"
> }


Hello. But, how do you check if it's below element 'Entry'? Even if
you do something like
XMLSTARTELEM == "Entry" { foundEntry = 1 }
XMLSTARTELEM == "Fourth" && foundEntry { found = 1 }
it only tells you that Awk has encountered element 'Entry' and then
element 'Fourth'.

--
William Park <>
Open Geometry Consulting, Toronto, Canada
 
Reply With Quote
 
=?ISO-8859-1?Q?R=E9mi_Peyronnet?=
Guest
Posts: n/a
 
      08-21-2004
> But it somehow doesn't work - I don't know why - probably, because I am
> quite new to this . Any idea?


1. The provided XML file is not well-formed :

> <Entries>
> <Entry Id=1>

^
Put this into quotes : Id="1" (same thing for Id="2")

> [...]
> <Entries>


Should close the tag : </Entries>

2. xsl:when and xsl;otherwise need to be in a xsl:choose element :

<xsl:choose>
<xsl:when .... > ... </xsl:when>
<xsltherwise> ... </xsltherwise>
</xsl:choose>

When these changes are done, this works perfectly

These typos are easily detectable with a xml editor (as cooktop by example).

Hth

--
Rémi Peyronnet
 
Reply With Quote
 
Tassilo J. Klein
Guest
Posts: n/a
 
      08-21-2004
Well, thanks - but the mistakes you mentioned I just forgot in the
message I posted but I actually had it in my .xsl file.

But thanks even though

Rémi Peyronnet wrote:
>> But it somehow doesn't work - I don't know why - probably, because I
>> am quite new to this . Any idea?

>
>
> 1. The provided XML file is not well-formed :
>
> > <Entries>
> > <Entry Id=1>

> ^
> Put this into quotes : Id="1" (same thing for Id="2")
>
> > [...]
> > <Entries>

>
> Should close the tag : </Entries>
>
> 2. xsl:when and xsl;otherwise need to be in a xsl:choose element :
>
> <xsl:choose>
> <xsl:when .... > ... </xsl:when>
> <xsltherwise> ... </xsltherwise>
> </xsl:choose>
>
> When these changes are done, this works perfectly
>
> These typos are easily detectable with a xml editor (as cooktop by
> example).
>
> Hth
>

 
Reply With Quote
 
kahrs
Guest
Posts: n/a
 
      08-21-2004
William Park wrote:

> Hello. But, how do you check if it's below element 'Entry'? Even if
> you do something like
> XMLSTARTELEM == "Entry" { foundEntry = 1 }
> XMLSTARTELEM == "Fourth" && foundEntry { found = 1 }
> it only tells you that Awk has encountered element 'Entry' and then
> element 'Fourth'.


Correct, your bash solutions uses the bash-internal element
stack and searches in it for "Fourth". In xmlgawk, I have
to emulate the element stack with gawk's associative arrays:

BEGIN { XMLMODE=1 ; found="Didn't find it" }

XMLSTARTELEM { elms[XMLSTARTELEM]=1 }

XMLENDELEM { delete elms[XMLSTARTELEM] }

XMLSTARTELEM == "Fourth" {
found="Found it"
if ("Entry" in elms)
print "'Fourth' is under 'Entry'"
else
print "'Fourth' is not under 'Entry'"
}

END { print found }
 
Reply With Quote
 
William Park
Guest
Posts: n/a
 
      08-21-2004
kahrs <> wrote:
> William Park wrote:
>
> > Hello. But, how do you check if it's below element 'Entry'? Even if
> > you do something like
> > XMLSTARTELEM == "Entry" { foundEntry = 1 }
> > XMLSTARTELEM == "Fourth" && foundEntry { found = 1 }
> > it only tells you that Awk has encountered element 'Entry' and then
> > element 'Fourth'.

>
> Correct, your bash solutions uses the bash-internal element
> stack and searches in it for "Fourth". In xmlgawk, I have
> to emulate the element stack with gawk's associative arrays:
>
> BEGIN { XMLMODE=1 ; found="Didn't find it" }
>
> XMLSTARTELEM { elms[XMLSTARTELEM]=1 }
>
> XMLENDELEM { delete elms[XMLSTARTELEM] }
>
> XMLSTARTELEM == "Fourth" {
> found="Found it"
> if ("Entry" in elms)


Okey, I got it. Much cleaner than I'd imagined.

> print "'Fourth' is under 'Entry'"
> else
> print "'Fourth' is not under 'Entry'"
> }
>
> END { print found }


--
William Park <>
Open Geometry Consulting, Toronto, Canada
 
Reply With Quote
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      08-23-2004
In article <cg5mdm$8nt$04$>,
Tassilo J. Klein <> wrote:
% Ooops, I forgot to mention, that I am using XLST to produce HTML and
% therefore want to make those checks using XLS.

An XPath which matches all Entry elements which contain a Fourth element is

//Entry[Fourth]

You can use this as part of a test, match, or select expression in XSLT. For
instance (you don't need // xsl:template's match attribute)

<xsl:template match='Entry[Fourth]'>
My favourite Entry is number <xsl:value-of select='@Id'/>.
</xsl:template>

or (ok, this isn't quite the same expression)

<xsl:template match='Entry'>
Blah blah blah
<xsl:if test='Fourth'>
This Entry has a Fourth element.
</xsl:if>
</xsl:template>

or how about

<xsl:template match='/'>
We do something with all Entry elements containing fourth:
<xsl:apply-templates mode='withfourth' select='//Entry[Fourth]'/>
Then we do something else with the whole document:
<xsl:apply-templates/>
</xsl:template>

<xsl:template mode='withfourth' match="*">
Blah blah blah.
</xsl:template>

...
--

Patrick TJ McPhee
East York Canada

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check process existence nkammah@yahoo.fr Java 3 04-17-2006 09:28 AM
Check for existence of an object? CK ASP .Net 5 03-21-2006 08:23 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
Is it possible to check the existence of a URL in asp.net? TaeHo Yoo ASP .Net 3 10-20-2003 09:14 PM
Is it possible to check the existence of a URL in asp.net? Leon ASP .Net 1 10-17-2003 11:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57