Search code examples
xpathxqueryosb

If Attribute value is not empty in given xml, get the attribute value


In the below XML I need attribute value (ID="5682094") if Type="CARMENT" from IdealId element

<ns0:doingRS xmlns:ns0="http://myorg.com" xmlns:ns2="http://commonOrg">
    <ns2:Pass/>
    <ns0:something>
        <ns0:some abc="false" xyz="false" prq="track" str="RACK" fdg="true" fgdg="true" fdfd="false" ewe="false" ytyt="false" trtrt="false" ewew="false" fdfdf="false" bgn="false" ujm="false" qa="false" qws="false" qwe="false" wqwqwqw="false">
            <ns2:SomeRates>
                <ns2:SomeRate CodeH="track" someCode="RACK">
                    <ns2:SomeProfile>
                        <ns2:SomeProfileList>
                            <ns2:IdealID Type="Prr" Ideal_CTX="ABC" ID="5033095"/>
                            <ns2:IdealID Type="CARMENT" Ideal_CTX="ABC" ID=""/>
                        </ns2:SomeProfileList>
                    </ns2:SomeProfile>
                    <ns2:SomeProfile>
                        <ns2:SomeProfileList>
                            <ns2:IdealID Type="Prr" Ideal_CTX="ABC" ID="5043839"/>
                            <ns2:IdealID Type="CARMENT" Ideal_CTX="ABC" ID=""/>
                        </ns2:SomeProfileList>
                    </ns2:SomeProfile>
                    <ns2:SomeProfile>
                        <ns2:SomeProfileList>
                            <ns2:IdealID Type="Prr" Ideal_CTX="ABC" ID="5682094"/>
                            <ns2:IdealID Type="CARMENT" Ideal_CTX="ABC" ID="5682094"/>
                        </ns2:SomeProfileList>
                    </ns2:SomeProfile>
                </ns2:SomeRate>
            </ns2:SomeRates>
        </ns0:some>
    </ns0:something>
</ns0:doingRS>

So in summary i need attribute value if and only if attribute is not empty. If there exists xpath/xquery to extract the attribute Id, please help me out


Solution

  • Select item with @Type="CARMENT" and then it's not-empty ID

    //*[@Type="CARMENT"]/@ID[not(.="")]