开发者

Use Xpath to find the appropriate element based on the element value

开发者 https://www.devze.com 2023-01-07 13:14 出处:网络
I have the following xml snippet <ZMARA01 SEGMENT=\"1\"> <CHARACTERISTICS_01>X,001,COLOR_ATTRIBUTE_FR,BRUN ÉCORCE,TMBR,French C</CHARACTERISTICS_01>

I have the following xml snippet

        <ZMARA01 SEGMENT="1">
            <CHARACTERISTICS_01>X,001,COLOR_ATTRIBUTE_FR,BRUN ÉCORCE,TMBR,French C</CHARACTERISTICS_01>
            <CHARACTERISTICS_02>X,001,COLOR_ATTRIBUTE,Timber开发者_如何学JAVA Brown,TMBR,Color Attr</CHARACTERISTICS_02>
        </ZMARA01>

I am looking for an xpath expression that will match based on COLOR_ATTRIBUTE. It will not always be in CHARACTERISTIC_02. It could be CHARACTERISTIC_XX. Also I don't want to match COLOR_ATTRIBUTE_FR. I have been using this:

Transaction.Input_XML{/ZMAT/IDOC/E1MARAM/ZMARA01/*[starts-with(local-name(.), 'CHARACTERISTIC_')][contains(.,'COLOR_ATTRIBUTE')]}

This gets me mostly there but it matches both COLOR_ATTRIBUTE and COLOR_ATTRIBUTE_FR


Use:

contains(concat(',', ., ','), ',COLOR_ATTRIBUTE,')

This first surrounds the string value of the context node with commas, then simply tests if the so cunstructed string contains ',COLOR_ATTRIBUTE,'.

Thus we treat all cases (pattern at the start of the string, pattern at the end of the string and pattern neither at the start or at the end) in the same single way.


If COLOR_ATTRIBUTE is guaranteed not to be in the first or last position, you could use [contains(.,',COLOR_ATTRIBUTE,')], otherwise you could use something like [contains(.,'COLOR_ATTRIBUTE') and not contains(.,'COLOR_ATTRIBUTE_FR')].

0

精彩评论

暂无评论...
验证码 换一张
取 消