Advertisements
I want to find the text value of a node that has a dot (".") in its name. I did not find a way to correctly escape the dot in my XPath expression. Here is a sample of my XML file:
<order>
<purchase-order-no>1412530</purchase-order-no>
<expected-delivery-date>2023-10-21</expected-delivery-date>
<supplier-no>600857</supplier-no>
<shop>
<code>123</code>
<name>Shop Name XYZ</name>
<street>Random Street 23</street>
<pc>9876</pc>
<city>CityABC</city>
</shop>
<lines>
<pos.>1</pos.>
<article-nr>5180063</article-nr>
<supplier-article-nr>523.442.0</supplier-article-nr>
<qty>1</qty>
<unit>PCE</unit>
<unit-price>5.44</unit-price>
<details>some optional details</details>
</lines>
<lines>
<pos.>2</pos.>
<article-nr>5185838</article-nr>
<supplier-article-nr>741.823.1</supplier-article-nr>
<qty>3</qty>
<unit>PCE</unit>
<unit-price>2.93</unit-price>
<details/>
</lines>
</order>
I would like to get the 1
and 2
values of each <pos.>
node.
Trying to escape the dot with \
or \\
did not work and gave me an Invalid character '\' in expression
error.
I am using a workaround using the order of the nodes:
//lines/node()[1]/text()
but I want to find a secure way of doing it. I do not control the format of the files I receive, and I cannot be sure that this order will stay so – and I cannot easily request to change it either.
>Solution :
local-name
will help here.
Try this:
//lines/*[local-name()='pos.']/text()