Handling multiple occurrences of the same tag in xml #1792
Replies: 2 comments
-
If you know a particular path is an array (but sometimes just has one thing), you can use this trick to ensure it's always an array:
This will set Hope that helps! I thought I had that as an example in the docs, but it seems I don't (sorry). Will add this is an example 👍🏼 |
Beta Was this translation helpful? Give feedback.
-
@mikefarah Thanks! That indeed works, I don't know how I missed it... Just to make it into a more complete thing, the magic expression that IMO would be very useful to show specifically when talking about parsing xml is:
which returns a list consistently, even when there are none. (I'd close it, but maybe you want to keep it open as a reminder...) |
Beta Was this translation helpful? Give feedback.
-
Please describe your feature request.
I wish I could use yq to handle xml inputs that might have multiple occurrences of the same tag.
(I'm not sure if an issue is the proper place or if there is some hack around it, feel free to point me elsewhere if so.)
The problem is that yq wraps multiple occurrences of a repeated tag as an array, but does not do that if there's a single occurrence.
A quick example of what this has lead me to -- given the following two files:
I want to get an output of the number of
bar
tags. The resulting fight I had with yq is borderline insanity. There are many factors that make it extremely difficult:Describe the solution you'd like
This is kind of difficult, since there's many different ways in which this could work:
Could have some flag that creates a consistent type: so parsing either of the above xmls would return the same structure (ie, dropping the "convenience" of not always using arrays).
There is a
length
, but it is happy to work on any input, so piping anything tolength
is a losing battle AFAICT. Could work if there was somepossibleArrayLength
for values that are possibly wrapped in an array, giving the length of the array or 1 if it wasn't wrapped. (ie, dropping the "convenient" aspect oflength
being defined on any input.)I even tried
| type
and quickly discovered that there's no if-then-else that can depend on it. The best I could get is pretty horrifying:and that still fails since in a single case the RHS is empty rather than
[]
.Beta Was this translation helpful? Give feedback.
All reactions