xmlxsltxslt-2.0

Need to pick the value which is not starting with particular letter


I'm having the input with combination of alphabets and numbers, So i just want to pick the value of first which shouldn't start with 'B'

input xml:

<Group>
    <map>
        <Num>B123456</Num>
    </map>
    <map>
        <Num>555555</Num>
    </map>
    <map>
        <Num>666666</Num>
    </map>
    <map>
        <Num>B65432</Num>
    </map>
</Group>

XSL I have tried:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output encoding="UTF-8" indent="yes" />
   <xsl:template match="Group">
       <groupNumber>
          <xsl:value-of select="map/Num[not(starts-with(., 'B'))][1]"/>
       </groupNumber>
   </xsl:template>
</xsl:transform>

Actual Output:

<groupNumber>555555 666666</groupNumber>

Expected output:

<groupNumber>555555</groupNumber>

I only need to get the first Num element value which is doesn't start with B


Solution

  • The problem with your expression:

    <xsl:value-of select="map/Num[not(starts-with(., 'B'))][1]"/>
    

    is that you are selecting every Num that doesn't start with "B" and is the first such child of its parent map.

    To select only the first such Num in the entire Group you could use:

    <xsl:value-of select="descendant::Num[not(starts-with(., 'B'))][1]"/>
    

    or a more efficient variant:

    <xsl:value-of select="map[not(starts-with(Num, 'B'))][1]/Num"/>