Reputation: 1077
I am dealing with some JSON-LD data in MarkLogic and have trouble using XPath on property names with "@" symbol. For example:
{
"@type": "News",
"title": "some title",
"description": "some description"
}
My goal is to retrieve the title if the type is "News". I understand "@" is reserved to represent attribute in XPath, so something below should not work.
doc.xpath('.[@type="News"]/title')
With the xdmp.encodeForNCName
function, I see the "@" symbol is represented as _40_
in the JSON representation. But it still doesn't work.
doc.xpath('.[_40_type="News"]/title')
Upvotes: 2
Views: 319
Reputation: 20414
While using fn:name()
would work too, as suggested by the other answers, you can address nodes with funny spelling in MarkLogic XPath directly too. Probably a deviation from the official XPath standard itself, but MarkLogic allows writing expressions like:
doc.xpath('node("@type")[. eq "News"]/title'
Very useful for JSON properties containing spaces and such as well..
HTH!
Upvotes: 2
Reputation: 14135
Here is the dirty solution.
.[@*[name() = '@type']][@*='News']/title
I know you are working with json, but I just checked the xpath in html with similar attribute and value combination. You can see the xpath considered both attribute name and value (as it's not selecting other node with the same name but different value).
Upvotes: 0
Reputation: 66783
You could test the name()
in a predicate:
doc.xpath('.[*[contains(name(), "@type")] = "News"]/title')
Upvotes: 1