Xpath and axis navigation using libxml

A

Alfie Noakes

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.
 
M

Martin Honnen

Alfie said:
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>
 
A

Alfie Noakes

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.


Alfie said:
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>
 
M

Martin Honnen

Alfie said:
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.
 
A

Alfie Noakes

Thanks Martin, I'll give it a go.


Alfie said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top