![]() |
|
|
|||||||
![]() |
XML - XSLT question: How to lookup another tag's children in XSLT |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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> yinglcs@gmail.com |
|
|
|
|
#2 |
|
Posts: n/a
|
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/ |
|
|
|
#3 |
|
Posts: n/a
|
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> > |
|
|
|
#4 |
|
Posts: n/a
|
Martin Honnen wrote: > 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> |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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" xmlns <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 |
|
|
|
#7 |
|
Posts: n/a
|
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 know how to go about it. [snip] Regards Jean-Francois Michaud |
|
|
|
#8 |
|
Posts: n/a
|
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: > 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" > xmlns > <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 |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
Joseph Kesselman wrote: > 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 |
|