I'm trying to iterate through repeating elements to extract data using
libxml2 but I'm having zero luck - any help would be appreciated.
My XML source is similar to the following - I'm trying to extract the
line number and product code from the repeating line elements:
<order xmlns="some-ns">
<header>
<orderno>123456 </orderno>
</header>
<lines>
<line>
<lineno>1</lineno>
<productcode>PE NS</productcode>
</line>
<line>
<lineno>2</lineno>
<productcode>ST APLER</productcode>
</line>
<line>
<lineno>3</lineno>
<productcode>RU LER</productcode>
</line>
</lines>
</order>
With the following code I can get at the non-repeating elements in the
header, and get the lines elements, but cannot extract the
lineno/productcode data via xpath:
XmlDoc = libxml2.parseFi le(XmlFile);
XPathDoc = XmlDoc.xpathNew Context();
XPathDoc.xpathR egisterNs('so', "some-ns");
# Extract data from the order header
PurchaseOrderNo =
XPathDoc.xpathE val('//so:order/so:header/so:orderno');
# Extract data from the order lines
for line in XPathDoc.xpathE val('//so:order/so:lines/so:line'):
print line.content;
# Explicitly free Xml document and XPath context
XmlDoc.freeDoc( )
XPathDoc.xpathF reeContext()
Ideally, I'd like to select the line data using xpath (similar to an
XSLT query after a 'for-each' - i.e. xpathEval('so:l ineno') and
xpathEval('so:p roductcode') once I've got the line element).
Any suggestions grealty appreciated!
Cheers, Nick.
libxml2 but I'm having zero luck - any help would be appreciated.
My XML source is similar to the following - I'm trying to extract the
line number and product code from the repeating line elements:
<order xmlns="some-ns">
<header>
<orderno>123456 </orderno>
</header>
<lines>
<line>
<lineno>1</lineno>
<productcode>PE NS</productcode>
</line>
<line>
<lineno>2</lineno>
<productcode>ST APLER</productcode>
</line>
<line>
<lineno>3</lineno>
<productcode>RU LER</productcode>
</line>
</lines>
</order>
With the following code I can get at the non-repeating elements in the
header, and get the lines elements, but cannot extract the
lineno/productcode data via xpath:
XmlDoc = libxml2.parseFi le(XmlFile);
XPathDoc = XmlDoc.xpathNew Context();
XPathDoc.xpathR egisterNs('so', "some-ns");
# Extract data from the order header
PurchaseOrderNo =
XPathDoc.xpathE val('//so:order/so:header/so:orderno');
# Extract data from the order lines
for line in XPathDoc.xpathE val('//so:order/so:lines/so:line'):
print line.content;
# Explicitly free Xml document and XPath context
XmlDoc.freeDoc( )
XPathDoc.xpathF reeContext()
Ideally, I'd like to select the line data using xpath (similar to an
XSLT query after a 'for-each' - i.e. xpathEval('so:l ineno') and
xpathEval('so:p roductcode') once I've got the line element).
Any suggestions grealty appreciated!
Cheers, Nick.
Comment