Skip to main content
Question

Weird behavior of XQueryExtractor

  • April 27, 2017
  • 4 replies
  • 41 views

Forum|alt.badge.img

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

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

4 replies

takashi
Celebrity
  • April 27, 2017

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
Celebrity
  • April 27, 2017

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?

 

 


Forum|alt.badge.img
  • Author
  • April 27, 2017

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).

 


takashi
Celebrity
  • April 27, 2017

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>