Connecting Tech Pros Worldwide Help | Site Map

Xpath and axis navigation using libxml

Alfie Noakes
Guest
 
Posts: n/a
#1: Nov 11 '08

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.

Martin Honnen
Guest
 
Posts: n/a
#2: Nov 11 '08

re: Xpath and axis navigation using libxml


Alfie Noakes wrote:
Quote:
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/
Alfie Noakes
Guest
 
Posts: n/a
#3: Nov 11 '08

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@yahoo.de>
wrote:
Quote:
>Alfie Noakes wrote:
>
Quote:
>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
Guest
 
Posts: n/a
#4: Nov 11 '08

re: Xpath and axis navigation using libxml


Alfie Noakes wrote:
Quote:
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
Quote:
Quote:
> <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/
Alfie Noakes
Guest
 
Posts: n/a
#5: Nov 11 '08

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@yahoo.de>
wrote:
Quote:
>Alfie Noakes wrote:
Quote:
>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
>
Quote:
Quote:
>> <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.
Closed Thread