1

I have the following XML structure:

<xml>
    <header>
        (Tags goes here)
    </header>
    <listItem>
        <id></id>
        ...
        (Sort of data)
        ...
        <arrayType1>
            <id></id>
            <parentId></parentId>
            <!-- Key: Data to lookup and get the entire structure (listItem) with XPath-->
            <key></key>
        </arrayType1>
        <arrayType2>
            <id></id>
            <parentId></parentId>
            <!-- Key: Data to lookup and get the entire structure (listItem) with XPath-->
            <key></key>
        </arrayType2>
    </listItem>
    <listItem>
        (Exactly same structure with different data as above)
    </listItem>
</xml>

Tag <parentId> has the same data of <listItem><id> tag (not to be confused with <id> of each arrayType).

I need a XPath expression that returns the <parentId> or <listItem><id> only having the data (or, by default, return the entire arrayTypeX and process later in another XSLT transformation).

I need this for a transformation in a XSLT Map in IBM Integration Designer (Websphere ESB), but i'm very confused about the XPath expression, but, the point is merely in build that XPath Expression.

I've tried with this expression:

/listItem[arrayType1[key = 'value']]/parentID

But i don't know if XPath supports that kind of syntax (array inside array looking for a specific value). That expression always return the first ID of the list.

The Cardinality for each item is:

header: [0..1] 
listItem: [0..*]
arrayType1: [0..*]
arrayType2: [0..*]

Can you shed some light about this?

Thanks

EDIT: This is an example:

<xml>
    <header>
        (Tags goes here)
    </header>
    <listItem>
        <id>1</id>
        <arrayType1>
            <id>1000</id>
            <parentId>1</parentId>
            <key>abc</key>
        </arrayType1>
        <arrayType1>
            <id>1500</id>
            <parentId>1</parentId>
            <key>mno</key>
        </arrayType1>
        <arrayType2>
            <id>2000</id>
            <parentId>1</parentId>
            <key>def</key>
        </arrayType2>
    </listItem>
    <listItem>
        <id>2</id>
        <arrayType1>
            <id>3000</id>
            <parentId>2</parentId>
            <key>ghi</key>
        </arrayType1>
        <arrayType2>
            <id>4000</id>
            <parentId>2</parentId>
            <key>jkl</key>
        </arrayType2>
    </listItem>
</xml>

Only having the <key> (for example, "ghi"), i expect to get the <id> or <parentId> "2", with an XPath expression.

2
  • please correct this line first: I need an XPath expression that returns the or only having the data and also, please provide a sample input/output that u're expecting Commented Jul 14, 2014 at 15:07
  • @sunbabaphu My mistake, it was an error in code format. Also i've attached an example of what i'm trying to get. Commented Jul 14, 2014 at 15:33

1 Answer 1

1

if the order of <parentId> and <key> is maintained, i.e. <parent> appears before <key> in each <arrayTypeX> then this will work:

//key[text()="value"]/preceding-sibling::parentId[1]  

for eg:
for key = "jkl" the return will be 2.

though, it must be mentioned here that, if required, it can be tweaked to account for multiple matches for <key> as well as no particular order for <parent> and <key>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.