FasterXML / jackson-future-ideas

Repository for SOLE PURPOSE of issue tracker and Wiki for NEW IDEAS. Please: NO BUG REPORTS.
18 stars 6 forks source link

Add support for xpath and xml attr. via @ #66

Closed JorgeCepeda closed 1 year ago

JorgeCepeda commented 1 year ago

In a project we're trying to interpret either Json or XML depending on what the user gives as entry data, along with some paths to the document.

We're using Jackson and the dataformat-xml and choose the mapper via a flag, but the path syntax doesn't seem to allow grabbing attributes with @, like in xpath syntax, example: xml = "<a b=\"attrib\"><b>node</b></a>"

and as path: "/a/@b" for the attribute and "/a/b" for the node

cowtowncoder commented 1 year ago

This would definitely need actual description of what is desired: title is quite ambiguous.

JorgeCepeda commented 1 year ago

I updated the description with an example of what I'm trying to do, I see for Json one option is JsonPath

https://github.com/json-path/JsonPath https://www.w3schools.com/xml/xpath_syntax.asp

pjfanning commented 1 year ago

Maybe https://cassiomolin.com/2016/07/13/using-jackson-and-json-pointer-to-query-and-parse-an-arbitrary-json-node/ is worth a read. If you Google Jackson and JSON pointer or Path, you will find lots of existing solutions.

JorgeCepeda commented 1 year ago

I'm able to read json, but I need to keep the node since the structure is arbitrary

cowtowncoder commented 1 year ago

My main issue is that XPath is strongly XML-specific, ditto for attributes. It will not be usable for things other than XML, since concept of attributes (f.ex) are XML-isms. As such I do not think JSON Pointer should have anything special beyond its (simple) specification.

But it does sound like this would be specific for Jackson XML format module (jackson-dataformat-xml), where it may or may not make sense. Jackson does not handle XML as DOM nodes, for example; and all logical manipulation occurs via streams of JsonTokens. Some information on attribute/element is retained, but not a lot.

At the same time, adding something to decorate attributes with @ prefix could be possible.

JorgeCepeda commented 1 year ago

As long as there is some xml attribute syntax it would suffice since json and xml have their own mapper

cowtowncoder commented 1 year ago

For suggestions for features to add to XML module, an issue would need to be created at

https://github.com/FasterXML/jackson-dataformat-xml/issues

JorgeCepeda commented 1 year ago

Side note: what is currently the way to do node.at("/a/b").asText() for that xml?

cowtowncoder commented 1 year ago

The original case is not something Jackson supports, due to name collision (or, rather, I think both attribute b and element bs values would be combined, depending on target type -- for JsonNode become an array value).

But if there was a feature to use @ prefix, you'd use

node.at("/a/@b").asText();

for attribute and

node.at("/a/b").asText()

for element.

JorgeCepeda commented 1 year ago

Okay, I'll open the issue soon then, thanks

cowtowncoder commented 1 year ago

Filed as:

https://github.com/FasterXML/jackson-dataformat-xml/issues/573

so closing this one.