Okay, so I’ve got some example xml that looks like this:
<Node name="details">
<Node name="1130482269">
<Val name="type"><u>2</u></Val>
<Val name="owner"><n/></Val>
<Val name="account_number"><u>99-71-39</u></Val>
</Node>
<Node name="570722270">
<Val name="type"><u>2</u></Val>
<Val name="owner"><n/></Val>
<Val name="account_number"><u>99 71 40</u></Val>
</Node>
<Node name="1845243341">
<Val name="type"><u>2</u></Val>
<Val name="owner"><n/></Val>
<Val name="account_number"><u>9971-41</u></Val>
</Node>
</Node>
I’m trying to do a search based on account_number. But there are so many different formats that it could be in. Some have spaces, some have dashes, some have a mixture etc. If I was able to do a search removing spaces and dashes, I should be able to get any account number.
With accountnumber being the search query, the ultimate goal is to get the second level node name number by doing a search in XPATH doing this:
doc = libxml2.parseDoc(xml)
ctxt = doc.xpathNewContext()
res = ctxt.xpathEval("/Node/Node[Val[@name='account_number']/*='" + str(accountnumber) + "']/@name")
Is there a way to use XPATH search, to remove all dashes and spaces, and leave only numbers?
Thanks
Yes, it is possible using
translate()function. Example:will output
997139.You can use then (code line broken for readability):
Note the white space included in the second argument of translate
'- '(or even'- ') necessary to remove white spaces.