Dipping my toe in a little Java at the minute and have a question about XPath.
I have a large Xml and I want to use XPath to be able to grab a specific node and then fire further XPath calls against this small chunk of Xml.
Here s rough outline of my Xml:
<Page>
<ComponentPresentations>
<ComponentPresentation>
<Component>
<Title>
<ComponentTemplate>
<ComponentPresentation>
<Component>
<Title>
<ComponentTemplate>
My first XPath selects the <Component> node based upon the value of a <ComponenTemplate> Id value:
String componentExpFormat = "/Page/ComponentPresentations/ComponentPresentation/ComponentTemplate/Id[text()='%1$s']/ancestor::ComponentPresentation";
String componentExp = String.format(componentExpFormat, template);
XPathExpression expComponent = xPath.compile(componentExp);
Node componentXml = (Node) expComponent.evaluate(xmldoc, XPathConstants.NODE);
This gives me the <Component> I want but I can;t seem to be able to then XPath against the Node:
String componentExpTitle = "/Component/Fields/item/value/Field/Name[text()='title']/parent::node()/Values/string";
XPathExpression expTitle = xPath.compile(componentExpTitle);
String eventName = expTitle.evaluate(componentXml, XPathConstants.STRING).toString();
Without this I'll have to include the full XPath each time:
/Page/ComponentPresentations/ComponentPresentation/ComponentTemplate/Id[text()='%1$s']/ancestor::ComponentPresentation/Component/Fields/item/value/Field/Name[text()='title']/parent::node()/Values/string
Is that the only way?
Cheers
componentExpTitleis an absolute path, remove the leading slash and try it.