Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT question: How to lookup another tag's children in XSLT

Reply
Thread Tools

XSLT question: How to lookup another tag's children in XSLT

 
 
yinglcs@gmail.com
Guest
Posts: n/a
 
      09-16-2006
Hi,
I have a newbie XSLT question. I have the following xml, and I would
like to
find out the children of feature element in each 'features' element.

i.e. for each <features> I would like to look up what each feature
depends on and gerenate a text file. For example, in the following
file, I would like to find out feature A depends on A1 and A2 and
feature B depends on B1 and B2. And write that to a text file.

How can I do that in XSLT? I know I can loop thru the 'features'
element using "for-each" but how can I do the lookup ?

Thanks for any help.

<feature name="A">
<depends ="A1"/>
<depends ="A2"/>
</feature>

<feature name="B">
<depends ="B1"/>
<depends ="B2"/>
</feature>

<features>
<feature name="A"/>
<feature name="B"/>
</features>

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      09-17-2006


http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:


> How can I do that in XSLT? I know I can loop thru the 'features'
> element using "for-each" but how can I do the lookup ?


You have not even shown a 'featutes' element. If you know xsl:for-each
why can't you use one xsl:for-each select="feature" and then inside that
another xsl:for-each select="depends"?

> <depends ="A1"/>

^^^^^^^^^^^^^^^^
This is not even XML.



--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Peter Flynn
Guest
Posts: n/a
 
      09-17-2006
(E-Mail Removed) wrote:
> Hi,
> I have a newbie XSLT question. I have the following xml


Your example is not XML. I suggest you redesign it so that it is.

Quite apart from that, you haven't shown us everything. You talk about
feature A depending on A1 and A2, but nowhere in your data is there
anything to say what A1 and A2 are.

You almost certainly don't need or want to "loop" through anything.
In XSLT, what you appear to want can be done much more simply with
templates.

But first you have to have a well-formed or valid XML document.

///Peter
--
XML FAQ: http://xml.silmaril.ie

and I would
> like to
> find out the children of feature element in each 'features' element.
>
> i.e. for each <features> I would like to look up what each feature
> depends on and gerenate a text file. For example, in the following
> file, I would like to find out feature A depends on A1 and A2 and
> feature B depends on B1 and B2. And write that to a text file.
>
> How can I do that in XSLT? I know I can loop thru the 'features'
> element using "for-each" but how can I do the lookup ?
>
> Thanks for any help.
>
> <feature name="A">
> <depends ="A1"/>
> <depends ="A2"/>
> </feature>
>
> <feature name="B">
> <depends ="B1"/>
> <depends ="B2"/>
> </feature>
>
> <features>
> <feature name="A"/>
> <feature name="B"/>
> </features>
>

 
Reply With Quote
 
=?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
Guest
Posts: n/a
 
      09-18-2006

Martin Honnen wrote:
> (E-Mail Removed) wrote:
>
>
> > How can I do that in XSLT? I know I can loop thru the 'features'
> > element using "for-each" but how can I do the lookup ?

>
> You have not even shown a 'featutes' element. If you know xsl:for-each
> why can't you use one xsl:for-each select="feature" and then inside that
> another xsl:for-each select="depends"?
>
> > <depends ="A1"/>

> ^^^^^^^^^^^^^^^^
> This is not even XML.


This is obviously a mistake. What he most probably meant is:

<feature name="A">
<depends name="A1"/>
<depends name="A2"/>
</feature>

<feature name="B">
<depends name="B1"/>
<depends name="B2"/>
</feature>

<features>
<feature name="A"/>
<feature name="B"/>
</features>

 
Reply With Quote
 
Joseph Kesselman
Guest
Posts: n/a
 
      09-18-2006
Jean-François Michaud wrote:
> This is obviously a mistake. What he most probably meant is:


Granted, but trying to help folks based on a guess tends to be less than
productive.

yinglcs, if you're still looking for help, post a corrected description
of your problem.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
 
Reply With Quote
 
=?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
Guest
Posts: n/a
 
      09-18-2006

(E-Mail Removed) wrote:
> Hi,
> I have a newbie XSLT question. I have the following xml, and I would
> like to
> find out the children of feature element in each 'features' element.
>
> i.e. for each <features> I would like to look up what each feature
> depends on and gerenate a text file. For example, in the following
> file, I would like to find out feature A depends on A1 and A2 and
> feature B depends on B1 and B2. And write that to a text file.
>
> How can I do that in XSLT? I know I can loop thru the 'features'
> element using "for-each" but how can I do the lookup ?
>
> Thanks for any help.
>
> <feature name="A">
> <depends ="A1"/>
> <depends ="A2"/>
> </feature>
>
> <feature name="B">
> <depends ="B1"/>
> <depends ="B2"/>
> </feature>
>
> <features>
> <feature name="A"/>
> <feature name="B"/>
> </features>


You should, before anything else, know that XSLT is not procedural
language; it is a functional language. Certain things that you might be
familiar with under procedural languages (like incrementing a variable
for example) is not directly possible with the facilities the language
offers. You have to break the natural processing progression and use
recursions to accomplish this particular feat.

A quick look at what you are trying to accomplish makes me think that
you might have to go through this recursively to accomplish what you
want to do.

if you HAVE to use the features element, you might have to use a
template that matches the features element to then recursively process
the entire XML tree that you would feed in as a parameter to a
recursive template, outputting what you want as you are going down the
recursion.

if you DON'T HAVE to use features,

You can simply use something like this:

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="feature">
<xsl:text>Feature </xsl:text><xsl:value-of select="@name" />
<xsl:text>Depends on </xsl:text>
<xsl:apply-templates />
</xsl:template>

<xsl:template match="depends">
<xsl:value-of select="@name" /><xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>

The XML snippet that you posted is malformed though, so I took the
liberty of correcting it:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<feature name="A">
<depends name="A1"/>
<depends name="A2"/>
</feature>

<feature name="B">
<depends name="B1"/>
<depends name="B2"/>
</feature>

<features>
<feature name="A"/>
<feature name="B"/>
</features>
</root>

I didn't test the code but this should fly .

Regards
Jean-Francois Michaud

 
Reply With Quote
 
=?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
Guest
Posts: n/a
 
      09-18-2006

Joseph Kesselman wrote:
> Jean-François Michaud wrote:
> > This is obviously a mistake. What he most probably meant is:

>
> Granted, but trying to help folks based on a guess tends to be less than
> productive.


Bah, not really. This is pretty straight forwards . He just doesn't
know how to go about it.

[snip]

Regards
Jean-Francois Michaud

 
Reply With Quote
 
yinglcs@gmail.com
Guest
Posts: n/a
 
      09-19-2006
Thanks for everybody's help. And thanks Jean-François Michaud for
providing me an example.

Jean-François Michaud,
I tried you example, I have 2 questions:
1. How can I make the children element 'feature' of 'features' NOT
match this '<xsl:template match="feature">' in my xslt?

<?xml version="1.0" encoding="UTF-8"?>
<root>
<feature name="A">
<depends name="A1"/>
<depends name="A2"/>
</feature>

<feature name="B">
<depends name="B1"/>
<depends name="B2"/>
</feature>

<features>
<feature name="A"/>
<feature name="B"/>
</features>
</root>

2. How can I just walk thru the children of 'features' and match each
one with this xml template? for example, if I only have :

<features>

<feature name="B"/>
</features>

I only print out
Feature BDepends on
B1
B2

where I have both:

<features>
<feature name="A"/>
<feature name="B"/>
</features>

I will print out both:
Feature ADepends on
A1
A2


Feature BDepends on
B1
B2

Jean-François Michaud wrote:
> (E-Mail Removed) wrote:
> > Hi,
> > I have a newbie XSLT question. I have the following xml, and I would
> > like to
> > find out the children of feature element in each 'features' element.
> >
> > i.e. for each <features> I would like to look up what each feature
> > depends on and gerenate a text file. For example, in the following
> > file, I would like to find out feature A depends on A1 and A2 and
> > feature B depends on B1 and B2. And write that to a text file.
> >
> > How can I do that in XSLT? I know I can loop thru the 'features'
> > element using "for-each" but how can I do the lookup ?
> >
> > Thanks for any help.
> >
> > <feature name="A">
> > <depends ="A1"/>
> > <depends ="A2"/>
> > </feature>
> >
> > <feature name="B">
> > <depends ="B1"/>
> > <depends ="B2"/>
> > </feature>
> >
> > <features>
> > <feature name="A"/>
> > <feature name="B"/>
> > </features>

>
> You should, before anything else, know that XSLT is not procedural
> language; it is a functional language. Certain things that you might be
> familiar with under procedural languages (like incrementing a variable
> for example) is not directly possible with the facilities the language
> offers. You have to break the natural processing progression and use
> recursions to accomplish this particular feat.
>
> A quick look at what you are trying to accomplish makes me think that
> you might have to go through this recursively to accomplish what you
> want to do.
>
> if you HAVE to use the features element, you might have to use a
> template that matches the features element to then recursively process
> the entire XML tree that you would feed in as a parameter to a
> recursive template, outputting what you want as you are going down the
> recursion.
>
> if you DON'T HAVE to use features,
>
> You can simply use something like this:
>
> XSLT:
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xsl:template match="/">
> <xsl:apply-templates />
> </xsl:template>
>
> <xsl:template match="feature">
> <xsl:text>Feature </xsl:text><xsl:value-of select="@name" />
> <xsl:text>Depends on </xsl:text>
> <xsl:apply-templates />
> </xsl:template>
>
> <xsl:template match="depends">
> <xsl:value-of select="@name" /><xsl:text> </xsl:text>
> </xsl:template>
> </xsl:stylesheet>
>
> The XML snippet that you posted is malformed though, so I took the
> liberty of correcting it:
>
> XML:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
> <feature name="A">
> <depends name="A1"/>
> <depends name="A2"/>
> </feature>
>
> <feature name="B">
> <depends name="B1"/>
> <depends name="B2"/>
> </feature>
>
> <features>
> <feature name="A"/>
> <feature name="B"/>
> </features>
> </root>
>
> I didn't test the code but this should fly .
>
> Regards
> Jean-Francois Michaud


 
Reply With Quote
 
Joseph Kesselman
Guest
Posts: n/a
 
      09-19-2006
(E-Mail Removed) wrote:
> 1. How can I make the children element 'feature' of 'features' NOT
> match this '<xsl:template match="feature">' in my xslt?


One of many ways would be to match on "/root/feature"


> 2. How can I just walk thru the children of 'features' and match each
> one with this xml template?


Inside the template for features, use xsl:apply-templates to process its
children.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
 
Reply With Quote
 
=?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
Guest
Posts: n/a
 
      09-21-2006

Joseph Kesselman wrote:
> (E-Mail Removed) wrote:
> > 1. How can I make the children element 'feature' of 'features' NOT
> > match this '<xsl:template match="feature">' in my xslt?

>
> One of many ways would be to match on "/root/feature"


To be more precise, what you would have to say to eliminate processing
of feature in features, what you would have to specify is that you want
the templates to do nothing when they encounter those specific
elements. As Joseph specified, you would reference feature within
features like this.

<xsl:template match="features/feature">
</xsl:template>

The template being empty makes it so that those particular feature
elements from the source XML tree will get dropped on the way to the
result XML tree.

> > 2. How can I just walk thru the children of 'features' and match each
> > one with this xml template?

>
> Inside the template for features, use xsl:apply-templates to process its
> children.


As Joseph said, if you specify the above template, using this template
also will make it so that when features is encountered, its children
will be processed using already defined templates:

<xsl:template match="features">
<xsl:apply-templates />
</xsl:template>

As things get more complex, you might have to specify priorities for
your templates if you have many templates "talking" about the same
element but under different conditions. If you want to be certain that
the feature element doesn't get processed by the wrong feature
template, you would set down a priority on the one you want executed in
priority. Like this:

<xsl:template match="features/feature" priority="1">
</xsl:template>

On a side note, unless you want to specify in the "features" element
less feature elements to be processed than the total amount of
"expanded" feature elements (feature containing depends) present in
your XML, then the "features" construct is probably not relevant. But
then again, I don't really know what you are trying to accomplish. If
you were to specify less feature elements to be processed in features
than the total amount of expanded feature elements, then you might have
to look at more complex logic revolving around features using recursive
templates.

Hope this helps .

Regards
Jean-Francois Michaud

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
XSLT adding children in an optional structure Herman Slagman XML 1 05-28-2009 10:24 AM
XSLT 1.0: Select children based on arbitrary node? Zachary Turner XML 1 05-30-2007 06:26 PM
XSLT: How to find a node containing TWO matching children? bearclaws XML 3 03-10-2005 08:11 PM
XSLT Select nodes without text-node children whose names starts with specifix text Michael Reiche XML 3 02-05-2004 10:40 PM



Advertisments