tags:

views:

36

answers:

2

I have the following XML:

        <ZMARA SEGMENT="1">
            <MATERIAL>000000000030001004</MATERIAL>
            <PRODUCT_GROUP>14000IAA</PRODUCT_GROUP>
            <PRODUCT_GROUP_DESC>HER 30 AR NEW Size</PRODUCT_GROUP_DESC>
            <CLASS_CODE>I046</CLASS_CODE>
            <CLASS_CODE_DESC>Heritage 30</CLASS_CODE_DESC>
            <CHARACTERISTICS_01>,001,PLANNING_ALERT_PERCENTAGE, 50.000,PLANNI</CHARACTERISTICS_01>
            <CHARACTERISTICS_02>X,001,COLOR_ATTRIBUTE,Weathered Wood,WEWD,Col</CHARACTERISTICS_02>
            <CHARACTERISTICS_03>,001,ARMA_UOM,SALES SQUARE,SSQ,ARMA UNIT OF M</CHARACTERISTICS_03>
            <CHARACTERISTICS_04>,001,ARMA_A_CATEGORY,05-Below 260 Lam/Multi-l</CHARACTERISTICS_04>
        </ZMARA>

Using XPath I need to select the CHARACTERISTICS_XX element whose value contains the COLOR_ATTRIBUTE token. It will not always be characteristics_02. Thanks for the help. I am a total noob at XPath.

+2  A: 

This should work:

//ZMARA/*[contains(.,'COLOR_ATTRIBUTE')]
spinon
Thanks for the help.
mpenrow
+4  A: 

This looks like its taken from a sap idoc, you can probably be lucky that the fieldnamed are not 6 character long abbreviations :)

The answer given by spinon is correct, however if there could be another element that contains the text 'COLOR_ATTRIBUTE', this would give a more specific match:

/ZMARA/*[starts-with(local-name(.), 'CHARACTERISTICS_')][contains(.,'COLOR_ATTRIBUTE')]

Another suggestion is to avoid the '//' expression if you know where the ZMARA element can occur, in the expression above ZMARA would only be searched as a root element which would be more performant.

Jörn Horstmann
This looks good. I'll use it. And yes this is SAP IDOC data. Thanks for the help.
mpenrow