Given an XML structure like so:
<?xml version="1.0" encoding="UTF-8"?>
<JSON>
<allFiles>
<_-Mna81v3-W_7WC1w69n9>
<name type="string">File1</name>
<reason type="string">Issue1</reason>
<status type="string">Pending</status>
<submissionTime type="string">11/3/2021, 10:11:37 AM</submissionTime>
</_-Mna81v3-W_7WC1w69n9>
<_-Mna83OkLF8vB8fV6CIy>
<name type="string">File2</name>
<reason type="string">Other</reason>
<status type="string">Pending</status>
<submissionTime type="string">11/3/2021, 10:11:44 AM</submissionTime>
</_-Mna83OkLF8vB8fV6CIy>
</allFiles>
</JSON>
As you can see the child nodes inside allFiles are generic names, how can I target them?
I tried to return them through the following
/JSON/allFiles[1]
File1 IssuePending11/3/2021, 10:11:37 AMFile2OtherPending11/3/2021, 10:11:44 AM
/JSON/allFiles/*
returns the first child node as the following
File1 Issue1Pending11/3/2021, 10:11:37 AM
I am confused how to target the child nodes of allFiles (_-Mna81v3-W_7WC1w69n9
and _-Mna83OkLF8vB8fV6CIy
) without mentioning their name in the command as they are generic and change.
CodePudding user response:
You can use /JSON/allFiles/*
to select all these elements.
I've seem some strange designs of XML document but this is a new one for me!
CodePudding user response:
/JSON/allFiles/*
Returns the first child node as the following
File1 Issue1Pending11/3/2021, 10:11:37 AM
/JSON/allFiles/*
does indeed select all children elements of allFiles
.
What you are seeing, however, is the string value of only the first such element. This would happen, for example, in XPath 1.0 when the result of /JSON/allFiles/*
, a nodeset, is passed to a function expecting a string. The standard behavior there for the function to operate on the string value of the first element — an understandably surprising result to the uninitiated.
Your options depend upon how you're using /JSON/allFiles/*
- In the context of the hosting language (XSLT, Python, Java, etc), you could iterate over the nodeset rather than passing it to a function expecting a string.
- In the context of an editor or other tool, you can often choose to view the results as markup rather than as text. (If the tool didn't take care to iterate the results and then convert to string values, it may suffer from the same limitation as described above.)
See also
I tried to return them through the following
/JSON/allFiles[1]
File1 IssuePending11/3/2021, 10:11:37 AMFile2OtherPending11/3/2021, 10:11:44 AM
That XPath selects the first allFile
child of JSON
. Your result again suggests that you're looking at the results as string values rather than as markup.