Define XPATH expressions
XPath stands for XML Path Language. XPath uses "path like" syntax to identify and navigate nodes in an XML document.
ONIXEDIT includes 2 different XPATH engines:
- The native XPATH engine from .NET which offers XPATH 1.0 compliance. This engine is used everywhere in ONIXEDIT.
- Saxon HE 9.8 that is Open Source under Mozilla Public License 2.0. Source code of Saxon HE 9.8 can be found here: https://sourceforge.net/projects/saxon/files/. Saxon XPATH engine offers XPATH 3.1 compliance and is used by our generic Export to Flat File or Excel tool as well as ONIXEDIT Scheduler.
For instance, you can set XPATH instructions when you manage columns of your ONIXEDIT Main list (XPATH 1.0) or when you export to a flat file (XPATH 3.1)
ONIXEDIT uses XPATH to select a particular value in an ONIX product that corresponds to very specific criteria. For instance, if you have multiple prices defined for a title and you want to show the USD price of a specific supplier in your main list, you must use XPath to do so. Otherwise, ONIXEDIT will present you the first price found in the SupplyDetail composite if you simply select the default PriceAmount tag. In the following case, it will be the CAD price:
<SupplyDetail>
<SupplierName>ONIXEDIT</SupplierName>
<ProductAvailability>21</ProductAvailability>
<Price>
<PriceTypeCode>01</PriceTypeCode>
<PriceAmount>6.99</PriceAmount>
<CurrencyCode>CAD</CurrencyCode>
</Price>
<Price>
<PriceTypeCode>01</PriceTypeCode>
<PriceAmount>4.99</PriceAmount>
<CurrencyCode>USD</CurrencyCode>
</Price>
</SupplyDetail>
But with a complete XPATH instruction, you can exactly identify the USD price that you need to show. For instance, the following XPATH 1.0 instructions indicate to consider the SupplyDetail composite having the SupplierName tag equals to "ONIXEDIT". Starting from that point, then drill down to the Price having the CurrencyCode tag equals to "USD" and get the corresponding PriceAmount value:
//Product/SupplyDetail[SupplierName='ONIXEDIT']/Price[CurrencyCode='USD']/PriceAmount
The following provides other XPATH 1.0 examples:
- //Product/MainSubject[MainSubjectSchemeIdentifier='93']/SubjectHeadingText: Shows the Thema Main Subject.
- //Product/Subject[SubjectSchemeIdentifier='20']/SubjectHeadingText: Shows keywords
- //Product/SupplyDetail/Price[PriceAmount>=100 and PriceTypeCode = '02' and CurrencyCode ='USD']/PriceAmount: Shows the USD PriceAmount tag value of the first RRP including taxes Price that is greater than or equals to 100.
You can also use specialized functions in your XPATH instructions. For instance:
- string-length(//Product/Contributor[CountryCode="CA"])>0: Shows "True" for each title having at least one Canadian contributor.
- concat(//Product/Title[TitleType='01']/TitlePrefix, ' ', //Product/Title[TitleType='01']/TitleWithoutPrefix): Shows the complete distinctive title obtained from the concatenation of TitlePrefix and TitleWithoutPrefix.
- concat(//Product/Title[TitleType='01']/TitlePrefix, ' ', //Product/Title[TitleType='01']/TitleWithoutPrefix): Shows "True" for titles having their Main BISAC subject beginning by "FIC" (i.e. "Fiction" titles).
XPATH 3.1:
Since version 3.4.8, ONIXEDIT includes Saxon HE 9.8. We added it mostly to provide advanced capabilities to our end users needing to produce very specific flat files or Excel export scripts.Thus, our generic export tool is now XPATH 3.1 compliant. This means that you can add XPATH columns in your export scripts with advanced XPATH syntax. For instance, you can set IF THEN ELSE expressions like this:
if(//Product/ProductForm='DG')
then 'NO HANDLING'
else 'HANDLING REQUIRED'
if(//Product/ProductForm='DG')
then 'NO HANDLING'
else 'HANDLING REQUIRED'
Nested IF THEN ELSE expressions are also supported. For instance, supposed that we want to export a column to a flat file with value "SCIENCE" when the BISAC main subject code begin by "SCI" and "RELIGION" when it begins by "REL". And for all other cases, we want to show "OTHERS: " followed by the subject headings. Here is the corresponding XPATH 3.1 expression:
if(contains(//Product/MainSubject[MainSubjectSchemeIdentifier="10"]/SubjectCode, "SCI"))
then "SCIENCE"
else if (contains(//Product/MainSubject[MainSubjectSchemeIdentifier="10"]/SubjectCode, "REL"))
then "RELIGION"
else concat("OTHERS: ", //Product/MainSubject[MainSubjectSchemeIdentifier="10"]/SubjectHeadingText)
Consider the following ONIX Contributor composites:
<Contributor>
<SequenceNumber>1</SequenceNumber>
<ContributorRole>A01</ContributorRole>
<NamesBeforeKey>Raoul</NamesBeforeKey>
<KeyNames>Guénette</KeyNames>
</Contributor>
<Contributor>
<SequenceNumber>2</SequenceNumber>
<ContributorRole>A01</ContributorRole>
<NamesBeforeKey>Maxime</NamesBeforeKey>
<KeyNames>Ménard</KeyNames>
</Contributor>
<Contributor>
<SequenceNumber>3</SequenceNumber>
<ContributorRole>A01</ContributorRole>
<NamesBeforeKey>Danny</NamesBeforeKey>
<KeyNames>Côté</KeyNames>
</Contributor>
Suppose that we want to export all the contributor names to a single column of a flat file and we want to present the Last Name first, followed by a comma and the First Name. To reach that goal, we will use a FOR instruction that will apply a "concat" function to each instance of "//Product/Contributor" composite. The corresponding XPATH expression will look like this:
for $x in //Product/Contributor
return concat($x/KeyNames, ', ', $x/NamesBeforeKey)
Notice that to make it work within an XPATH column of an ONIXEDIT export script, you must select the option "Process all values" at the bottom of the XPATH column dialogue:
For the above ONIX snippet, the result will then be:
XPath references:
- Basic syntax: https://www.w3schools.com/xml/xpath_syntax.asp
- Functions: https://www.w3schools.com/xml/xsl_functions.asp
- Advanced XPATH 3.1 syntax documentation from Saxon: https://www.saxonica.com/html/documentation/expressions