Question

Weird behavior of XQueryExtractor

  • 27 April 2017
  • 4 replies
  • 2 views

Badge

I am given the following XML document:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Line>
    <Segments>
        <Segment>
            <Inputs>
                <Input id="718">
                    <Attribute id="1300">ABC</Attribute>
                    <Attribute id="1" >DEF</Attribute>
                    <Attribute id="2">GHI</Attribute>
                </Input>
            </Inputs>
        </Segment>
        <Segment>
            <Inputs>
                <Input id="718">
                    <Attribute id="1300">ABC2</Attribute>
                    <Attribute id="1" >DEF</Attribute>
                    <Attribute id="2">GHI2</Attribute>
                </Input>
            </Inputs>
        </Segment>
    </Segments>
</Line>

I would like to access DEF. Using xmlstarlet: 

 

xml sel -t -v "//Input[@id=718][1]/Attribute[@id=1]" query-testing.xml

I get the result "DEF" (the original document had many input nodes with id "718", that's why I added "[1]" after "[@id=718]").

However, invoking XQueryExtractor with "//Input[@id=718][1]/Attribute[@id=1]" gives the result

<Attribute id="1">DEF</Attribute>

Of course I can modify that answer to get the desired DEF, but I am just wondering if I didn't configure the transformer correctly.

Attached is a screenshot of my configuration and the workbench: 

0684Q00000ArEHsQAN.png


4 replies

Userlevel 2
Badge +17

Hi @john_leo, your XQuery expression is an XPath indicating the element node. If you need to extract the content (text node) of the element, try setting this expression.

//Input[@id=718][1]/Attribute[@id=1]/text()

In addition, if possible values of the id attribute within the Input element are unique for each Input element, this expression is also available.

//Input[@id=718]/Attribute[@id=1]/text()
Userlevel 2
Badge +17

Hi @john_leo, your XQuery expression is an XPath indicating the element node. If you need to extract the content (text node) of the element, try setting this expression.

//Input[@id=718][1]/Attribute[@id=1]/text()

In addition, if possible values of the id attribute within the Input element are unique for each Input element, this expression is also available.

//Input[@id=718]/Attribute[@id=1]/text()
Ah, I missed that you have already mentioned that the id attribute in the Input element is not unique in fact. The second expression in my post won't work for the actual document.

 

I've never used the xmlstarlet, but -v option seems to indicate that the command extracts the value (text) of the element specified with the XPath. Isn't it?

 

 

Badge

Hi @john_leo, your XQuery expression is an XPath indicating the element node. If you need to extract the content (text node) of the element, try setting this expression.

//Input[@id=718][1]/Attribute[@id=1]/text()

In addition, if possible values of the id attribute within the Input element are unique for each Input element, this expression is also available.

//Input[@id=718]/Attribute[@id=1]/text()
@takashi thanks, that worked. I am also not too expert on xmlstarlet, but I guess -v is for "value". I guess that was my mistake.

 

What confuses me is that with your first example I actually get "DEF" more than once (the original has more such nodes, I have updated the xml document accordingly).

 

Userlevel 2
Badge +17

Hi @john_leo, your XQuery expression is an XPath indicating the element node. If you need to extract the content (text node) of the element, try setting this expression.

//Input[@id=718][1]/Attribute[@id=1]/text()

In addition, if possible values of the id attribute within the Input element are unique for each Input element, this expression is also available.

//Input[@id=718]/Attribute[@id=1]/text()
This expression could work.

 

(//Input[@id=718])[1]/Attribute[@id=1]/text()
[Addition] I think the first expression in my previous post could also work as expected if the multiple Input elements are contained by the same parent element like this.

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Line>
    <Segments>
        <Segment>
            <Inputs>
                <Input id="718">
                    <Attribute id="1300">ABC</Attribute>
                    <Attribute id="1" >DEF</Attribute>
                    <Attribute id="2">GHI</Attribute>
                </Input>
                <Input id="718">
                    <Attribute id="1300">ABC2</Attribute>
                    <Attribute id="1" >DEF2</Attribute>
                    <Attribute id="2">GHI2</Attribute>
                </Input>
            </Inputs>
        </Segment>
    </Segments>
</Line>

Reply