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

Discussion in 'XML' started by yinglcs@gmail.com, Sep 16, 2006.

  1. Guest

    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>
     
    , Sep 16, 2006
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Sep 17, 2006
    #2
    1. Advertising

  3. Peter Flynn Guest

    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>
    >
     
    Peter Flynn, Sep 17, 2006
    #3
  4. 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>
     
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Sep 18, 2006
    #4
  5. 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
     
    Joseph Kesselman, Sep 18, 2006
    #5
  6. 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="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
     
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Sep 18, 2006
    #6
  7. 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
     
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Sep 18, 2006
    #7
  8. Guest

    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="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
     
    , Sep 19, 2006
    #8
  9. 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
     
    Joseph Kesselman, Sep 19, 2006
    #9
  10. 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
     
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Sep 21, 2006
    #10
  11. wrote:
    > 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


    And this is where the $^@! hit the fan under XSLT. The language doesn't
    naturally lend itself naturally to certain forms of non hierarchical
    processing.

    Since your features element is hierarchically dissociated structurally
    from the extended feature element, there is no easy way to take care of
    this other than passing down the whole XML source tree as a parameter
    to other template processing (maybe even using a named template instead
    of a match template.

    What you will want to do is pass down the whole XML source tree as a
    parameter down to the features and have template process each feature
    within features using the source XML tree passed as a parameter to do
    your processing. This form of processing will somewhat break the
    normal flow of XSLT so be very thorough with your logic.

    You might be able to get away with this by simply referencing the
    parent nodes under a for-each in features also.

    To make things very easy, if you were to structure things this way in
    your XML (if you can), all the problems would dissapear:

    XML:

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

    Features contains individual feature which depends on certain things

    Your XSLT templates would clearly be simplified. The idea would be to
    only have features contain the feature that you want to display.

    [snip]

    Regards
    Jean-Francois Michaud
     
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Sep 21, 2006
    #11
  12. Guest

    Jean-François, Joseph,

    Thanks for your help. I need to think about it further about what you
    guys teach me.
    But the problem is, I can't do this suggestion from Jean-François:

    > To make things very easy, if you were to structure things this way in
    > your XML (if you can),



    Thank you again.


    Jean-François Michaud wrote:
    > wrote:
    > > 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

    >
    > And this is where the $^@! hit the fan under XSLT. The language doesn't
    > naturally lend itself naturally to certain forms of non hierarchical
    > processing.
    >
    > Since your features element is hierarchically dissociated structurally
    > from the extended feature element, there is no easy way to take care of
    > this other than passing down the whole XML source tree as a parameter
    > to other template processing (maybe even using a named template instead
    > of a match template.
    >
    > What you will want to do is pass down the whole XML source tree as a
    > parameter down to the features and have template process each feature
    > within features using the source XML tree passed as a parameter to do
    > your processing. This form of processing will somewhat break the
    > normal flow of XSLT so be very thorough with your logic.
    >
    > You might be able to get away with this by simply referencing the
    > parent nodes under a for-each in features also.
    >
    > To make things very easy, if you were to structure things this way in
    > your XML (if you can), all the problems would dissapear:
    >
    > XML:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <root>
    > <features>
    > <feature name="A">
    > <depends name="A1"/>
    > <depends name="A2"/>
    > </feature>
    > <feature name="B">
    > <depends name="B1"/>
    > <depends name="B2"/>
    > </feature>
    > </features>
    > </root>
    >
    > Features contains individual feature which depends on certain things
    >
    > Your XSLT templates would clearly be simplified. The idea would be to
    > only have features contain the feature that you want to display.
    >
    > [snip]
    >
    > Regards
    > Jean-Francois Michaud
     
    , Sep 22, 2006
    #12
  13. Guest

    Thanks for all the help. However, I need more help.

    Basically, I would like to get each 'feature' child of 'features' and

    For example, for this input:

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

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

    <feature name="C">
    <depends name="C1"/>
    <depends name="C2"/>
    </feature>

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

    I would like to generate this output:
    Feature A Depends on A1
    Feature A Depends on A2

    Feature B Depends on B1


    Here is my xslt:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/features">
    <xsl:for-each select="feature">
    <xsl:call-template name="feature">

    </xsl:call-template>
    </xsl:for-each>
    </xsl:template>



    <xsl:template name="feature">
    <xsl:param name="dummy" />
    <xsl:text>Feature </xsl:text><xsl:value-of select="@name"
    />
    <xsl:text>Depends on </xsl:text>

    </xsl:if>
    </xsl:template>

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


    </xsl:stylesheet>


    But this just match each 'feature' element in the source.

    My questions are:

    1. if I do this:
    <xsl:template name="feature"> ... </xsl:template>
    intead of
    <xsl:template match="feature"> ... </xsl:template>

    I don't match any 'feature' element. But the output show that i only
    match the 'feature'. Here is the xslt output. That is not i would like
    to accomplish.
    A1
    A2



    B1
    B2



    C1
    C2

    2. Why this does not match anything? I expect this will loop thru each
    children of "features" and call the name template 'feature'. But
    apparently, I did something wrong.

    <xsl:template match="/features">
    <xsl:for-each select="feature">
    <xsl:call-template name="feature">

    </xsl:call-template>
    </xsl:for-each>
    </xsl:template>

    I appreciate if anyone can give me more help.

    Thank you.


    wrote:
    > Jean-François, Joseph,
    >
    > Thanks for your help. I need to think about it further about what you
    > guys teach me.
    > But the problem is, I can't do this suggestion from Jean-François:
    >
    > > To make things very easy, if you were to structure things this way in
    > > your XML (if you can),

    >
    >
    > Thank you again.
    >
    >
    > Jean-François Michaud wrote:
    > > wrote:
    > > > 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

    > >
    > > And this is where the $^@! hit the fan under XSLT. The language doesn't
    > > naturally lend itself naturally to certain forms of non hierarchical
    > > processing.
    > >
    > > Since your features element is hierarchically dissociated structurally
    > > from the extended feature element, there is no easy way to take care of
    > > this other than passing down the whole XML source tree as a parameter
    > > to other template processing (maybe even using a named template instead
    > > of a match template.
    > >
    > > What you will want to do is pass down the whole XML source tree as a
    > > parameter down to the features and have template process each feature
    > > within features using the source XML tree passed as a parameter to do
    > > your processing. This form of processing will somewhat break the
    > > normal flow of XSLT so be very thorough with your logic.
    > >
    > > You might be able to get away with this by simply referencing the
    > > parent nodes under a for-each in features also.
    > >
    > > To make things very easy, if you were to structure things this way in
    > > your XML (if you can), all the problems would dissapear:
    > >
    > > XML:
    > >
    > > <?xml version="1.0" encoding="UTF-8"?>
    > > <root>
    > > <features>
    > > <feature name="A">
    > > <depends name="A1"/>
    > > <depends name="A2"/>
    > > </feature>
    > > <feature name="B">
    > > <depends name="B1"/>
    > > <depends name="B2"/>
    > > </feature>
    > > </features>
    > > </root>
    > >
    > > Features contains individual feature which depends on certain things
    > >
    > > Your XSLT templates would clearly be simplified. The idea would be to
    > > only have features contain the feature that you want to display.
    > >
    > > [snip]
    > >
    > > Regards
    > > Jean-Francois Michaud
     
    , Oct 1, 2006
    #13
  14. roy axenov Guest

    Please don't top-post.

    wrote:
    > For example, for this input:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <root>
    > <feature name="A">
    > <depends name="A1"/>
    > <depends name="A2"/>
    > </feature>
    >
    > <feature name="B">
    > <depends name="B1"/>
    > </feature>
    >
    > <feature name="C">
    > <depends name="C1"/>
    > <depends name="C2"/>
    > </feature>
    >
    > <features>
    > <feature name="A"/>
    > <feature name="B"/>
    > </features>
    > </root>
    >
    > I would like to generate this output:
    > Feature A Depends on A1
    > Feature A Depends on A2
    >
    > Feature B Depends on B1
    >
    > Here is my xslt:
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


    What about the output method? (And XML declaration, for
    that matter.) Since you seem to be trying to output text,
    perhaps you should give a hint about that to your XSLT
    processor - and to other people's XSLT processors?

    > <xsl:template match="/features">
    > <xsl:for-each select="feature">


    Why?

    > <xsl:call-template name="feature">
    > </xsl:call-template>


    I don't believe that's what <xsl:call-template> is for.

    > </xsl:for-each>
    > </xsl:template>
    >
    > <xsl:template name="feature">


    And what's that supposed to match?

    > <xsl:param name="dummy" />
    > <xsl:text>Feature </xsl:text>
    > <xsl:value-of select="@name" />
    > <xsl:text>Depends on </xsl:text>
    > </xsl:if>


    That's not even well-formed.

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


    And how's that supposed to work?

    > </xsl:stylesheet>
    >
    > But this just match each 'feature' element in the source.


    No, it matches all the <depends> elements in your XML,
    because your third template is the only one that actually
    does anything. Try removing the first two templates, and
    you'll get exactly the same result.

    > 1. if I do this:
    > <xsl:template name="feature"> ... </xsl:template>
    > intead of
    > <xsl:template match="feature"> ... </xsl:template>
    >
    > I don't match any 'feature' element.


    Precisely. You don't match anything. So the template
    doesn't do anything.

    > But the output show that i only match the 'feature'.


    No, it doesn't. It shows something entirely different. See
    above.

    > Here is the xslt output. That is not i would like
    > to accomplish.
    > A1
    > A2
    >
    >
    >
    > B1
    > B2
    >
    >
    >
    > C1
    > C2


    For that matter, it's NOT the XSLT output, either. First,
    your XSLT is not well-formed, and even if it were
    well-formed, there's no sign of B2 anywhere in the XML.
    (But that's to be expected; B2's are supposed to be
    awfully stealthy, after all.)

    If you really want to get help on the newsgroup, I'd
    heartily recommend posting your actual code, as well as
    actual examples of your input/output. It seems just a bit
    disrespectful -- you're asking people for help, but you
    can't be bothered to weed out even the most obvious bugs
    (such as that </xsl:if>) in your code.

    No one's asking you to post your production code or
    anything. Just spend a minute or two making sure what
    you're posting is relevant to your question. I spent five
    minutes of my Sunday morning straightening out your WAG's,
    and if I started tinkering with your code in a pretty
    cheerful mood, I'm now feeling quite murderous.

    > 2. Why this does not match anything? I expect this will
    > loop thru each children of "features" and call the name
    > template 'feature'. But apparently, I did something
    > wrong.


    That's not how it works. But since you couldn't be bothered
    to double-check what you post, I can't be bothered to
    explain that in detail to you. Read the spec. Or the
    reference. Whatever.

    The following works, but you'll just have to figure out why
    yourself. It'll do you a world of good, I think.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="text"/>
    <xsl:template match="/features">
    <xsl:apply-templates select="feature"/>
    </xsl:template>
    <xsl:template match="features/feature">
    <xsl:apply-templates
    select="/root/feature[@name=current()/@name]/depends"
    mode="list-dependencies">
    <xsl:with-param name="p-node" select="."/>
    </xsl:apply-templates>
    <xsl:text>
    </xsl:text>
    </xsl:template>
    <xsl:template match="depends" mode="list-dependencies">
    <xsl:param name="p-node"/>
    <xsl:text>Feature </xsl:text>
    <xsl:value-of select="$p-node/@name"/>
    <xsl:text> depends on </xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:text>
    </xsl:text>
    </xsl:template>
    </xsl:stylesheet>

    --
    roy axenov
     
    roy axenov, Oct 1, 2006
    #14
  15. Roy: you might want to look up the description of named templates, since
    you're off-base on that point.

    yinglcs: He does have a point; you'll get more and better help if you
    don't post obviously sloppy examples.


    > <xsl:template name="feature"> ... </xsl:template>
    > intead of
    > <xsl:template match="feature"> ... </xsl:template>
    > I don't match any 'feature' element.


    If you want the template to be executed by apply-templates matching, you
    must use match= with a match pattern. If you want it to run when you
    invoke it explicitly by name with call-template, you must use name= with
    a literal name. If you want the same template to be invoked both ways,
    you need to specify both. In this case, you seem to want it running only
    with call-template, though I don't quite see why you're doing it that way.

    >2. Why this does not match anything? I expect this will loop thru each
    >children of "features" and call the name template 'feature'. But
    >apparently, I did something wrong.
    > <xsl:template match="/features">


    /features matches <features> if and only if it's the outermost element
    (child of root). Get rid of the /.
     
    Joe Kesselman, Oct 1, 2006
    #15
  16. roy axenov Guest

    Joe Kesselman wrote:
    > yinglcs wrote:
    > Roy: you might want to look up the description of named
    > templates, since you're off-base on that point.


    Thanks for the pointer, I'm just starting with XSLT myself.
    (I have a feeling I'm going to need it soon, so I'm trying
    to be prepared ahead of time.)

    > > <?xml version="1.0" encoding="UTF-8"?>
    > > <root>
    > > . . .
    > > <features>
    > > . . .
    > > </features>
    > > </root>
    > >
    > > 2. Why this does not match anything? I expect this will
    > > loop thru each children of "features" and call the name
    > > template 'feature'. But apparently, I did something
    > > wrong.
    > > <xsl:template match="/features">

    >
    > /features matches <features> if and only if it's the
    > outermost element (child of root). Get rid of the /.


    Oh, I completely missed that. That's the problem with the
    XSLT I posted, too. It works (sort of), but the first
    template doesn't actually match anything because of this
    bug.

    --
    roy axenov
     
    roy axenov, Oct 1, 2006
    #16
  17. Guest

    Roy, Joe,

    Thanks for your help.

    Roy,
    As you said you start learning XSLT yourself, can you please tell me
    what do you read to learn XSLT?

    Thank you.
    roy axenov wrote:
    > Joe Kesselman wrote:
    > > yinglcs wrote:
    > > Roy: you might want to look up the description of named
    > > templates, since you're off-base on that point.

    >
    > Thanks for the pointer, I'm just starting with XSLT myself.
    > (I have a feeling I'm going to need it soon, so I'm trying
    > to be prepared ahead of time.)
    >
    > > > <?xml version="1.0" encoding="UTF-8"?>
    > > > <root>
    > > > . . .
    > > > <features>
    > > > . . .
    > > > </features>
    > > > </root>
    > > >
    > > > 2. Why this does not match anything? I expect this will
    > > > loop thru each children of "features" and call the name
    > > > template 'feature'. But apparently, I did something
    > > > wrong.
    > > > <xsl:template match="/features">

    > >
    > > /features matches <features> if and only if it's the
    > > outermost element (child of root). Get rid of the /.

    >
    > Oh, I completely missed that. That's the problem with the
    > XSLT I posted, too. It works (sort of), but the first
    > template doesn't actually match anything because of this
    > bug.
    >
    > --
    > roy axenov
     
    , Oct 2, 2006
    #17
  18. roy axenov Guest

    A: Because it messes up the order in which people normally
    read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on USENET?

    wrote:
    > roy axenov wrote:
    > > Joe Kesselman wrote:


    [named templates]

    > > > Roy: you might want to look up the description of
    > > > named templates, since you're off-base on that point.

    > >
    > > Thanks for the pointer, I'm just starting with XSLT
    > > myself. (I have a feeling I'm going to need it soon, so
    > > I'm trying to be prepared ahead of time.)
    > >

    > As you said you start learning XSLT yourself, can you
    > please tell me what do you read to learn XSLT?


    comp.text.xml, naturally. Lots of problems to solve here,
    and lots of useful hints, too.

    Now that I think of it, a short XSLT/XPath reference on
    developer.mozilla.org has been quite helpful.

    W3C recommendations are probably worth reading. They tend
    to put me to sleep in a couple of minutes or so, though.

    --
    roy axenov
     
    roy axenov, Oct 3, 2006
    #18
  19. > W3C recommendations are probably worth reading. They tend
    > to put me to sleep in a couple of minutes or so, though.


    If you're looking for something nearly as authoritative as the W3C spec,
    but readable by folks who aren't Language Lawyers, Mike Kay's tome on
    XSLT is still a remarkably good reference. Definitely something a
    beginner should have available, but probably not the place to start
    learning.

    If what you're looking for is tutorial material, I can't advise you on
    books. Do I need to plug IBM's XML DeveloperWorks website yet again?


    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Oct 4, 2006
    #19
    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. shruds
    Replies:
    1
    Views:
    841
    John C. Bollinger
    Jan 27, 2006
  2. Michael Reiche
    Replies:
    3
    Views:
    10,858
    Michael Reiche
    Feb 5, 2004
  3. bearclaws
    Replies:
    3
    Views:
    582
    bearclaws
    Mar 10, 2005
  4. Zachary  Turner
    Replies:
    1
    Views:
    529
    Bjoern Hoehrmann
    May 30, 2007
  5. Robert Karlsson
    Replies:
    1
    Views:
    129
Loading...

Share This Page