Options for looping through children with different element names

Discussion in 'XML' started by johkar, Apr 2, 2009.

  1. johkar

    johkar Guest

    Assume XML parent element has lots of children that are all unique
    element names. Is the only way to loop through them to use a wildcard
    (*) for the element name in the for-each or apply-templates call? I
    am looking for efficiency because I am going to have to do this many
    times in the XSLT and there could be hundreds of child element names
    for any given parent element. Note, I don't have control over the XML
    tree structure.

    Xpath: /category/businesstype/*

    <category>
    <businesstype>
    <specialty>Some text</specialty>
    <bigbox>Some text></bigbox>
    <internetonly>Some text</internetonly>
    ...many many more nodes
    </businesstype>
    </category>

    Any pointers appreciated.
     
    johkar, Apr 2, 2009
    #1
    1. Advertising

  2. johkar wrote:
    > Assume XML parent element has lots of children that are all unique
    > element names. Is the only way to loop through them to use a wildcard
    > (*) for the element name in the for-each or apply-templates call? I
    > am looking for efficiency because I am going to have to do this many
    > times in the XSLT and there could be hundreds of child element names
    > for any given parent element. Note, I don't have control over the XML
    > tree structure.
    >
    > Xpath: /category/businesstype/*
    >
    > <category>
    > <businesstype>
    > <specialty>Some text</specialty>
    > <bigbox>Some text></bigbox>
    > <internetonly>Some text</internetonly>
    > ...many many more nodes
    > </businesstype>
    > </category>


    I don't see any problem with using * if you want to process all element
    child nodes.
    If you prefer you can name each element (type) explicitly e.g. in 2.0
    you can do
    <xsl:template match="businesstype">
    <xsl:apply-templates select="speciality, bigbox, internetonly"/>
    </xsl:template>
    or in 1.0 (and of course 2.0) you can do e.g.
    <xsl:template match="businesstype">
    <xsl:apply-templates select="speciality | bigbox | internetonly"/>
    </xsl:template>
    You would need to test which version performs better with the XSLT
    processor of your choice.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 2, 2009
    #2
    1. Advertising

  3. johkar

    johkar Guest

    On Apr 2, 5:56 am, Martin Honnen <> wrote:
    > johkar wrote:
    > > Assume XML parent element has lots of children that are all unique
    > > element names.  Is the only way to loop through them to use a wildcard
    > > (*) for the element name in the for-each or apply-templates call?  I
    > > am looking for efficiency because I am going to have to do this many
    > > times in the XSLT and there could be hundreds of child element names
    > > for any given parent element.  Note, I don't have control over the XML
    > > tree structure.

    >
    > > Xpath:   /category/businesstype/*

    >
    > > <category>
    > >       <businesstype>
    > >               <specialty>Some text</specialty>
    > >               <bigbox>Some text></bigbox>
    > >               <internetonly>Some text</internetonly>
    > >               ...many many more nodes
    > >       </businesstype>
    > > </category>

    >
    > I don't see any problem with using * if you want to process all element
    > child nodes.
    > If you prefer you can name each element (type) explicitly e.g. in 2.0
    > you can do
    >   <xsl:template match="businesstype">
    >    <xsl:apply-templates select="speciality, bigbox, internetonly"/>
    >   </xsl:template>
    > or in 1.0 (and of course 2.0) you can do e.g.
    >   <xsl:template match="businesstype">
    >    <xsl:apply-templates select="speciality | bigbox | internetonly"/>
    >   </xsl:template>
    > You would need to test which version performs better with the XSLT
    > processor of your choice.
    >
    > --
    >
    >         Martin Honnen
    >        http://JavaScript.FAQTs.com/- Hide quoted text -
    >
    > - Show quoted text -


    Thanks for confirming and the tips.
     
    johkar, Apr 2, 2009
    #3
    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. Carl
    Replies:
    0
    Views:
    535
  2. Matt
    Replies:
    2
    Views:
    868
    Ben Edgington
    Oct 12, 2004
  3. Replies:
    5
    Views:
    495
    bouton
    Sep 22, 2005
  4. Replies:
    2
    Views:
    522
  5. WC Justice

    Build SQL string looping through field names

    WC Justice, Jan 24, 2005, in forum: ASP General
    Replies:
    2
    Views:
    199
    Bob Barrows [MVP]
    Jan 24, 2005
Loading...

Share This Page