Home > Blockchain >  How to format xpath values as map?
How to format xpath values as map?

Time:05-05

I am using the Xpath Notebook extension for VSCode. I am trying to extract information and get them, ideally as a table. So my goal is to map the selected data to a map like this:

Array<{
  name: string,
  text: string
}>

The input xml looks like this:

<Values>
    <Value name="TestValue">
        <Text text="Test text"/>
    </Value>
    <Value name="Another">
        <Text text="Another test"/>
    </Value>
</Values>

My code snippet looks like this:

doc("C:\..pathto..\test.xml")//Value/[@name!string(),./Text/@text!string()]

The output looks like this:

[
    [
        "TestValue",
        "Test text"
    ],
    [
        "Another",
        "Another test"
    ]
]

So I get all the data I need, but in the format I need it. Is there any simple way to map it to the format I have shown above?

I already tried some testing with map:entry, but so far I have not had any luck.

CodePudding user response:

The syntax for an XPath 3.1 map is e.g. map { 'name' : string(@name), 'text' : string(Text/@text) } so in the context of your sample I guess you want an array of maps with e.g.

doc("C:\..pathto..\test.xml")/Values ! array { Value ! map { 'name' : string(@name), 'text' : string(Text/@text) } }
  • Related