XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.
XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The must useful path expressions are listed below
Expression | Description |
---|---|
nodename | Selects all nodes with the name "nodename" |
/ | Selects from the root node |
// | Selects nodes in the document from the current node that match the selection no matter where they are |
. | Selects the current node |
.. | Selects the parent of the current node |
@ | Selects attributes |
In the table below we have listed some path expressions and the result of the expressions:
Path Expression | Result |
---|---|
doc | Selects all nodes with the name "doc" |
/doc | Selects the root element doc If the path starts with a slash ( / ) it always represents an absolute path to an element! |
doc/connectorSpecific/field | Selects all field elements that are children of connectorSpecific |
//field | Selects all field elements no matter where they are in the document |
doc/connectorSpecific//field | Selects all field elements that are descendant of the connectorSpecific element, no matter where they are under the connectorSpecific element |
//@name | Selects all attributes that are named name |
Predicates are used to find a specific node or a node that contains a specific value.
Predicates are always embedded in square brackets.
In the table below we have listed some path expressions with predicates and the result of the expressions:
Path Expression | Result |
---|---|
doc/connectorSpecific/field[1] | Selects the first field element that is the child of the connectorSpecific element. |
doc/connectorSpecific/field[@name] | Selects all the field elements that have an attribute named name |
doc/connectorSpecific/field[@name='id'] | Selects all the field elements that have a "name" attribute with a value of "id" |
AXPath myXPath = AXPathFactory.newInstance("components/component/config/agency"); String agency = myXPath.getString(someDOMElementOrAspireObject);
This is a simple groovy script that you can use in a workflow to retrieve data from your doc.
import com.searchtechnologies.aspire.framework.AXPathFactory; import com.searchtechnologies.aspire.services.AXPath; AXPath xPathId = AXPathFactory.newInstance("doc/connectorSpecific/field[@name='id']"); String id = xPathId.getString(doc); AXPath xPathDisplayName = AXPathFactory.newInstance("doc/connectorSpecific/field[@name='displayName']"); String displayName = xPathDisplayName.getString(doc); println(id + " - " + displauyName);
Remember to import the AXPathFactory and the AXPath clases in your groovy script
This is a regular doc file, that you can access from your workflow
<doc> <id>c:\dev\Custom-theme.css</id> <fetchUrl>file:/c:/dev/Custom-theme.css</fetchUrl> <url>file://c:/dev/Custom-theme.css</url> <displayUrl>c:\dev\Custom-theme.css</displayUrl> <lastModified>2016-04-22T19:57:35Z</lastModified> <dataSize>20132</dataSize> <acls/> <action>add</action> <connectorSpecific type="filesystem"> <field name="id">2568545</field> <field name="displayName">Custom-theme.css</field> <field name="isContainer">false</field> </connectorSpecific> <docType>item</docType> <sourceName>File System Source2</sourceName> <sourceId>File_System_Source2</sourceId> <repItemType>aspire/file</repItemType> <hierarchy> <item id="ED49128EDF0AFC48229B2DE7696F573B" level="3" name="Custom-theme.css" url="file://c:/dev/Custom-theme.css"> <ancestors> <ancestor id="BEEC3D649254F4E80658B633C58680C7" level="2" name="dev" parent="true" url="c:\dev"/> <ancestor id="B039D5CCDC96EE3CA35D2105DE66E182" level="1" url="c:\"/> </ancestors> </item> </hierarchy> <protocol source="FetchURL/protocol">file</protocol> <mimeType source="FetchURL/mimeType">content/unknown</mimeType> <extension source="FetchURL"> <field name="modificationDate">2016-04-22T19:57:35Z</field> <field name="content-length">20132</field> <field name="last-modified">Fri, 22 Apr 2016 19:57:35 GMT</field> <field name="content-type">content/unknown</field> </extension> <contentType source="ExtractTextStage/Content-Type">text/plain; charset=windows-1252</contentType> <extension source="ExtractTextStage"> <field name="X-Parsed-By">org.apache.tika.parser.DefaultParser</field> <field name="Content-Encoding">windows-1252</field> <field name="resourceName">file://c:/dev/Custom-theme.css</field> </extension> <content source="ExtractTextStage"><![CDATA[ ::-webkit-input-placeholder { color: #979797 !important; } :-moz-placeholder { /* Firefox 18- */ color: #979797 !important;; } ::-moz-placeholder { /* Firefox 19+ */ color: #979797 !important;; } :-ms-input-placeholder { color: #979797 !important;; } . . . ]]></content> </doc>