I have this xml response...
<otherDataFreetext>
<freetextDetail>
<subjectQualifier>3</subjectQualifier>
<type>P06</type>
</freetextDetail>
<longFreetext>PAX 057-3263559627-28/ETAF/BRL3284.98/27FEB13/SAOFJ2100/57515496</longFreetext>
... and I want get from 'longFreetext' the '057-3263559627' specific value.
Which is the correct XPath or XQuery I use?
Using XPath 2.0 (or XQuery 1.0 or newer which contains XPath as a subset) you could use tokenize to split up the sequence and reconstruct it.
string-join(//longFreetext/tokenize(., "[- ]")[position() = (2, 3)], "-")
Related
I am trying to use xpath to set an exchange property from the value of one element, depending on what the value is in another element...
So in my case i want an xpath to identify the PartyID where the RoleCode = 60. And another xpath expression to find the PartyID for where the RoleCode = Z60.
I would have used [1] etc but the send cannot guarantee the order the segments will be in.
Help is much appreciated!
<RegisteredProductCollection>
<RegisteredProduct>
<Status>1</Status>
<AddressLine2>test</AddressLine2>
<AddressLine1>37</AddressLine1>
<RegisteredProductPartyInformation>
<RegisteredProductPartyInformation>
<PartyID>9000028253</PartyID>
<RoleCode>60</RoleCode>
</RegisteredProductPartyInformation>
<RegisteredProductPartyInformation>
<PartyID>1288219</PartyID>
<RoleCode>Z60</RoleCode>
</RegisteredProductPartyInformation>
</RegisteredProductPartyInformation>
<PostalCode>3200</PostalCode>
<Country>NZ</Country>
<ProductID>G02411</ProductID>
<SerialID>1234124124</SerialID>
<City>test</City>
<ReferenceDate>20200514000000</ReferenceDate>
<District>wai</District>
</RegisteredProduct>
</RegisteredProductCollection>
You can use these XPath-1.0 expressions:
//RegisteredProductPartyInformation[RoleCode='60']/PartyID
and
//RegisteredProductPartyInformation[RoleCode='Z60']/PartyID
Change the ' to " if necessary.
I am trying to use XPath 1.0 to select the maximum double value in an XML document. I understand that XPath 1.0 doesn't support the Max function that would make this extremely easy, but the system I'm using only supports XPath 1.0 functions.
I'm not able to post the entire file due to length, but here is a sample of the document I'm scanning: https://codebeautify.org/xmlviewer/cbc93c7e.
I've tried this XPath Expression:
/site/forecast/datum[not(primary < preceding-sibling::datum/primary) and not (primary < following-sibling::datum/primary)]
But no matter what, it returns the wrong datum. It always returns:
<datum>
<valid timezone="UTC">2019-04-04T06:00:00-00:00</valid>
<primary name="Stage" units="ft">9.80</primary>
<secondary name="Flow" units="cfs">3010.00</secondary>
<pedts>HGIFE</pedts>
</datum>
I expect it to return the element with the maximum "primary stage" value like:
<datum>
<valid timezone="UTC">2019-03-28T18:00:00-00:00</valid>
<primary name="Stage" units="ft">13.10</primary>
<secondary name="Flow" units="cfs">13400.00</secondary>
<pedts>HGIFE</pedts>
</datum>
I understand it will return all those with the 13.10 primary stage, but that's okay. I'll select the first one it returns if I can just get it to that point.
Do note that you might have more than one maximum. Use:
/site/forecast/datum[not(primary < ../datum/primary)][1]
This select the element
<datum>
<valid timezone="UTC">2019-03-28T18:00:00-00:00</valid>
<primary name="Stage" units="ft">13.10</primary>
<secondary name="Flow" units="cfs">13400.00</secondary>
<pedts>HGIFE</pedts>
</datum>
Test it in http://www.xpathtester.com/xpath/4256aac47f4f0efc36e23a2f16183d90
I'm trying to select all elements using XSL and XPath syntax where there is more than one pickup. I'd like to return the counter_name for each. Can someone please help me with the syntax? In this example there is only one counter_name with pickup locations, but there could be multiple locations where there are pickup counters.
XPATH
<xsl:value-of select="results/unique_locations/partner_location_ids[count(pickup) > 0]/counter_name" /><br/>
XML
<results>
<unique_locations>
<counter_name>Lake Buena Vista, FL</counter_name>
<is_airport>N</is_airport>
<partner_location_ids>
<pickup>
</pickup>
<dropoff>
<container>ZR-ORLS001</container>
<container>ET-ORLR062</container>
<container>HZ-ORLS011</container>
<container>HZ-ORLW015</container>
<container>AV-ORLR004</container>
</dropoff>
</partner_location_ids>
<counter_name>Orlando, FL</counter_name>
<is_airport>N</is_airport>
<partner_location_ids>
<pickup>
<container>ET-ORLC037</container>
<container>AV-ORLC021</container>
<container>ET-ORLC033</container>
<container>ET-ORLC035</container>
<container>HZ-ORLS007</container>
<container>HZ-ORLC004</container>
<container>HZ-ORLC002</container>
<container>ZR-ORLS002</container>
<container>BU-ORLE002</container>
<container>AV-ORLC019</container>
<container>ET-ORLR064</container>
<container>ET-ORLC001</container>
<container>ET-ORLR063</container>
<container>ET-ORLR061</container>
<container>HZ-ORLC011</container>
<container>HZ-ORLC054</container>
<container>HZ-ORLN003</container>
<container>HZ-ORLC007</container>
<container>HZ-ORLC005</container>
<container>ZA-ORLC002</container>
<container>ZA-ORLC003</container>
<container>ZA-ORLC001</container>
<container>AV-ORLC002</container>
<container>AV-ORLC001</container>
<container>BU-ORLS001</container>
<container>ET-ORLC012</container>
<container>AL-ORLR071</container>
<container>HZ-ORLC022</container>
<container>ET-ORLC051</container>
<container>HZ-ORLC025</container>
<container>HZ-ORLN018</container>
<container>HZ-ORLC017</container>
<container>AV-ORLN003</container>
<container>BU-ORLC002</container>
<container>BU-ORLC003</container>
<container>BU-ORLS006</container>
<container>ET-ORLC027</container>
<container>ET-ORLC022</container>
<container>AL-ORLR081</container>
<container>BU-ORLC005</container>
<container>HZ-ORLR029</container>
<container>HZ-ORLC032</container>
<container>HZ-ORLC031</container>
<container>HZ-ORLC030</container>
<container>ET-ORLC021</container>
</pickup>
<dropoff>
<container>HZ-ORLC003</container>
<container>ZA-ORLC004</container>
<container>BU-ORLW002</container>
<container>HZ-ORLC026</container>
<container>ZR-ORLC010</container>
<container>AL-ORLR073</container>
</dropoff>
</partner_location_ids>
</unique_locations>
Your XML structure is non-ideal, in that it appears to contain elements that are associated with each other by sequence, rather than exclusively by containment within the same element. But XPath can deal with that.
Supposing that the context node for evaluation of the XPath is the parent node of the <results> whose contents you are examining, it appears you want something along these lines:
results/unique_locations/partner_location_ids[pickup/*]/preceding-sibling::counter_name
Note in the first place the predicate: [pickup/*]. The expression within, interpreted in boolean context, evaluates to true if the expression matches any nodes. That's why we need pickup/*, not just pickup, to distinguish between <pickup> elements that contain child nodes and those that don't.
Additionally, observe the use of the preceding-sibling axis instead of the default child axis to step from each matching <partner_location_ids> to its corresponding (I think) <counter_name>.
I use this XPath
//#getparam
to get this list of URL
/blabla/bla.php?id=100001&target=lala&new=lulu
/blabla/bla.php?id=100002&target=lala&new=lulu
/blabla/bla.php?id=100003&target=lala&new=lulu
I want to get list of IDs, and try this XPath
substring-before(substring-after(//#getparam, "?id="), "&")
but it only returns first ID. How to get all IDs only using XPath?
If you're using XPath 2.0, the following will do the trick:
//#getparam/substring-before(substring-after(., "?id="), "&")
If you're using XPath 1.0, there is no single expression to return the desired result set. You can iterate over each result of //#getParam and apply the functions.
I'm pulling up images dynamically based on EmployerName field using the below function.
concat("C:\Projects\GlobalResourceSet\Images\",$EmployerName,"_banner_2013.png")
It works fine.
But, If the EmployerName is wrong or its empty I should display a default image.
How can we do that using both Xpath 1.0 & 2.0
Any help will be thankful...
There's a rather bizarre solution for XPath 1.0 like this:
substring(S, 1, string-length(S) * number(C))
where S is a string and C is a boolean condition. If C is false, number(C) is 0, so nothing is output. If C is true, number(C) is 1, so the whole string is output. So the effect is "if condition C is true then output S else output nothing"; and you can combine this with another expression using the inverse condition to output different strings based on the value of the condition.
Or you could move to XPath 2.0, where life is much more boring.
In XPath 2.0, you could also do following:
concat("C:\Projects\GlobalResourceSet\Images\",
($EmployerName[. != ''], 'default')[1],
"_banner_2013.png"
)
This puts both the employer name (if set) and the "default image name" in a sequence and then selects the first of both.
Newlines added for readability, can be removed arbitrarily.
In xpath 2.0 this could work
if ($EmployerName = "") then "C:\Projects\GlobalResourceSet\Images\default.png" else concat("C:\Projects\GlobalResourceSet\Images\",$EmployerName,"_banner_2013.png")