Xpath and axis navigation using libxml

Discussion in 'XML' started by Alfie Noakes, Nov 11, 2008.

  1. Alfie Noakes

    Alfie Noakes Guest

    Using this Xpath expression,

    //table[@name='animals']/row

    I have a nodeset result from a Xpath query which returns all the <row>
    elements for the given table in the following simeple XML
    structure.....

    <database>

    [ other tables occur first ]

    <table name="animals">
    <row>
    <field name="name">Monkey</field>
    <field name="size">Biggish</field>
    </row>
    <row>
    <field name="name">Whale</field>
    <field name="size">Massive</field>
    </row>

    etc ...


    My question is, what is the best way to step through all the field
    elements in each row? I can do it in the software stepping through the
    sibling and child nodes but these seems cumbersome. I suspect there's
    something I can do using axis. Am I right?

    Regards to all.
    Alfie Noakes, Nov 11, 2008
    #1
    1. Advertising

  2. Alfie Noakes wrote:

    > My question is, what is the best way to step through all the field
    > elements in each row? I can do it in the software stepping through the
    > sibling and child nodes but these seems cumbersome. I suspect there's
    > something I can do using axis. Am I right?



    Are you using XSLT? Then you can do
    <xsl:for-each select="//table[@name='animals']/row">
    <xsl:for-each select="field">
    ...
    </xsl:for-each>
    </xsl:for-each>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Nov 11, 2008
    #2
    1. Advertising

  3. Alfie Noakes

    Alfie Noakes Guest

    There's no XSLT being used, though I have an XSD to validate against
    when the XML is loaded by the libxml library. After loading I need to
    build SQL command strings to add each row to a mySQL database. I'm
    going to have another crack at it today.


    On Tue, 11 Nov 2008 13:01:12 +0100, Martin Honnen <>
    wrote:

    >Alfie Noakes wrote:
    >
    >> My question is, what is the best way to step through all the field
    >> elements in each row? I can do it in the software stepping through the
    >> sibling and child nodes but these seems cumbersome. I suspect there's
    >> something I can do using axis. Am I right?

    >
    >
    >Are you using XSLT? Then you can do
    > <xsl:for-each select="//table[@name='animals']/row">
    > <xsl:for-each select="field">
    > ...
    > </xsl:for-each>
    > </xsl:for-each>
    Alfie Noakes, Nov 11, 2008
    #3
  4. Alfie Noakes wrote:
    > There's no XSLT being used, though I have an XSD to validate against
    > when the XML is loaded by the libxml library.


    Well even if you don't use XSLT as the host language for your XPath
    expressions the expressions below are still what you need

    >> <xsl:for-each select="//table[@name='animals']/row">
    >> <xsl:for-each select="field">


    meaning you first use //table[@name='animals']/row, the for each row
    element in that node set you evaluate the relative XPath expression
    field
    with the row element as the context node. I don't know the libxml API so
    I can't help with a concrete code sample.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Nov 11, 2008
    #4
  5. Alfie Noakes

    Alfie Noakes Guest

    Thanks Martin, I'll give it a go.


    On Tue, 11 Nov 2008 14:21:16 +0100, Martin Honnen <>
    wrote:

    >Alfie Noakes wrote:
    >> There's no XSLT being used, though I have an XSD to validate against
    >> when the XML is loaded by the libxml library.

    >
    >Well even if you don't use XSLT as the host language for your XPath
    >expressions the expressions below are still what you need
    >
    >>> <xsl:for-each select="//table[@name='animals']/row">
    >>> <xsl:for-each select="field">

    >
    >meaning you first use //table[@name='animals']/row, the for each row
    >element in that node set you evaluate the relative XPath expression
    > field
    >with the row element as the context node. I don't know the libxml API so
    >I can't help with a concrete code sample.
    Alfie Noakes, Nov 11, 2008
    #5
    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. Replies:
    0
    Views:
    918
  2. Ian Gregory
    Replies:
    1
    Views:
    503
  3. Olav
    Replies:
    3
    Views:
    4,232
  4. Vaucher Bastien

    Dealing with xpath using libxml

    Vaucher Bastien, Feb 21, 2007, in forum: Ruby
    Replies:
    0
    Views:
    92
    Vaucher Bastien
    Feb 21, 2007
  5. Stanislaw Wozniak
    Replies:
    7
    Views:
    336
    Matt Neuburg
    Aug 3, 2009
Loading...

Share This Page