New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flatten XML Parser? #309
Comments
FWIW this is how I'm using - name: Check if port 5665 is open from the master node
ansible.builtin.command: "nmap -oX - -p 5665 {{ inventory_hostname }}"
check_mode: false
changed_when: false
delegate_to: "{{ icinga_master_node }}"
register: icinga_nmap_xml
- name: Set a fact for the nmap output
community.general.xml:
xmlstring: "{{ icinga_nmap_xml.stdout }}"
xpath: "/nmaprun/host/ports/port[@portid='5665']/state"
content: attribute
register: icinga_port_state_results
- name: Set a fact for the port 5665 state
ansible.builtin.set_fact:
icinga_port_state: "{{ icinga_port_state_results | community.general.json_query('matches[].state | [0].state') }}"
- name: Port 5665 on the agent node needs to be open to accept connections from the master node
ansible.builtin.assert:
that:
- icinga_port_state is defined
- icinga_port_state == "open" |
Hey there! Is XML flattening basically taking attributes like this:
and turning them into elements, like this?
What is the actual difference in the JSON as output by |
Yes, that's it basically, the |
Ah, gotcha. There are a few options:
We just need to be aware that by not having any prefix character at all there could be collisions between attribute and element names, so maybe we should just change it to something that doesn't conflict with JMESPath, et al. Something like There is also a |
I don't know to be honest, I think I'd expect the I'm not sure about adding prefixes, I wonder how the XSLT stylesheet I've been using deals with attribute and element name collisions… I guess I should test it… |
Let me know what happens with duplicate attribute/element names. Yeah, does make more sense the other way, but that would break existing scripts, so don't want to do that. |
Having a element named the same as an attribute doesn't seen to be an issue for the XSLT stylesheet, for example this XML: <?xml version="1.0" encoding="UTF-8"?>
<nmaprun>
<host host="galaxy.ansible.com">
<status state="up" reason="syn-ack" reason_ttl="0"/>
<address addr="104.26.0.234" addrtype="ipv4"/>
<hostnames>
<hostname name="galaxy.ansible.com" type="user"/>
</hostnames>
</host>
</nmaprun> When transformed using this XSLT stylesheet like this: xsltproc attributes2elements.xslt galaxy.ansible.com.xml | xmllint --pretty 1 - Results in this XML: <?xml version="1.0"?>
<nmaprun>
<host>
<host>galaxy.ansible.com</host>
<status>
<state>up</state>
<reason>syn-ack</reason>
<reason_ttl>0</reason_ttl>
</status>
<address>
<addr>104.26.0.234</addr>
<addrtype>ipv4</addrtype>
</address>
<hostnames>
<hostname>
<name>galaxy.ansible.com</name>
<type>user</type>
</hostname>
</hostnames>
</host>
</nmaprun> And transforming this into YML using: xsltproc attributes2elements.xslt galaxy.ansible.com.xml | xmllint --pretty 1 - | jc --xml -py Looks like this: ---
nmaprun:
host:
host: galaxy.ansible.com
status:
state: up
reason: syn-ack
reason_ttl: '0'
address:
addr: 104.26.0.234
addrtype: ipv4
hostnames:
hostname:
name: galaxy.ansible.com
type: user Compared with the original XML transformed into YAML using: cat galaxy.ansible.com.xml | xmllint --pretty 1 - | jc --xml -py Results in this: ---
nmaprun:
host:
'@host': galaxy.ansible.com
status:
'@state': up
'@reason': syn-ack
'@reason_ttl': '0'
address:
'@addr': 104.26.0.234
'@addrtype': ipv4
hostnames:
hostname:
'@name': galaxy.ansible.com
'@type': user So there doesn't appear to be a problem with element and attributes having the same name, but perhaps I misunderstood the potential problem? |
How about something like this?
For example:
|
Ah, yes, I see, this:
Compared with:
Dunno… 🤔 |
Yeah, I think I'll make the |
No but that doesn't mean much! |
I have updated the https://github.com/kellyjonbrazil/jc/blob/dev/jc/parsers/xml.py |
Released in |
XML that makes use of a lot of attributes, rather than elements, results in JSON that is hard to work with using Ansible / JMESPath, for example
nmap
has XML output (but not JSON):nmap -oX - -p 443 galaxy.ansible.com | xmllint --pretty 1 -
Convert this into JSON / YAML and the results are not great...
However if the XML is flattened using XSLT first:
You then have something that is nicer to work with:
xsltproc attributes2elements.xslt galaxy.ansible.com.xml | jc --xml -py
So I was wondering if a
---xml-flatten
parser that first used XSLT to flatten XML might be something that could be considered?The text was updated successfully, but these errors were encountered: