Xpath and axis navigation using libxml

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alfie Noakes

    Xpath and axis navigation using libxml


    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">Mon key</field>
    <field name="size">Big gish</field>
    </row>
    <row>
    <field name="name">Wha le</field>
    <field name="size">Mas sive</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.

  • Martin Honnen

    #2
    Re: Xpath and axis navigation using libxml

    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

    Comment

    • Alfie Noakes

      #3
      Re: Xpath and axis navigation using libxml


      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 <mahotrash@yaho o.de>
      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>

      Comment

      • Martin Honnen

        #4
        Re: Xpath and axis navigation using libxml

        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

        Comment

        • Alfie Noakes

          #5
          Re: Xpath and axis navigation using libxml



          Thanks Martin, I'll give it a go.


          On Tue, 11 Nov 2008 14:21:16 +0100, Martin Honnen <mahotrash@yaho o.de>
          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.

          Comment

          Working...