Link to home
Start Free TrialLog in
Avatar of tesmc
tesmcFlag for United States of America

asked on

XSL: obtain last word in a text

I have the following XML

<ServInfo CMD="011">
      <Serv Code="ED" Type="RED">
      <Text>AS KK1 06L18MAR/CHAIR/ACCPT/USD2000/100RC CCVIXXXXXXXXXXXX0001EXPXXXX 8312312270746</Text>
      </Serv>
      
I want to obtain the the last field in <text> = 8312312270746. I have following but I don't think I"m referencing the @CMD correctly bc it errors out.

<xsl:variable name="test"  select ="//Itin/ServInfo[Serv[@Code='ED' and @Type = 'RED']/@CMD=$id/Text "  />

where $id = 11
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

<xsl:variable name="test"  select ="//Itin/ServInfo[Serv[@Code='ED' and @Type = 'RED']][@CMD=$id]/Text "  />

but I would force the attribute to a number to make sure it is numeric equality you get

<xsl:variable name="test"  select ="//Itin/ServInfo[Serv[@Code='ED' and @Type = 'RED']][number(@CMD) = number($id)]/Text "  />
for getting the last component in XSLT2
<xsl:value-of select="tokenize(normalize-space($test), ' ')[last()]"/>

in XSLT1 you will need to use recursion
Avatar of tesmc

ASKER

i used XSLT1.

<xsl:variable name="test"  select ="//Itin/ServInfo[Serv[@Code='ED' and @Type = 'RED']][number(@CMD) = number($id)]/Text "  />

and the Text wasn't found.
Avatar of tesmc

ASKER

XML input:
<ServInfo CMD="011">
      <Serv Code="ED" Type="RED">
      <Text>AS KK1 06L18MAR/CHAIR/ACCPT/USD2000/100RC CCVIXXXXXXXXXXXX0001EXPXXXX 8312312270746</Text>
      </Serv>
</ServInfo>      

If I do
<xsl:variable name="test"  select ="//Itin/ServInfo[Serv[@Code='ED' and @Type = 'RED']]/Serv/Text "  />
I can obtain all instances of Text where condition is satisfied.

But once i add the filter of the @CMD, nothing is found.  
<xsl:variable name="test"  select ="//Itin/ServInfo[Serv[@Code='ED' and @Type = 'RED']][number(@CMD) = number($id)]/Text "  />
Avatar of tesmc

ASKER

nvmd, i got it to work with
    <xsl:variable name="test"  select ="//Itin/ServInfo[Serv[@Code='ED' and @Type = 'RED']][number(@CMD) = number($id)]/Serv/Text "  />


but now i just need to obtain the last string of that text node =8312312270746
Text is not a child of ServInfo, you need a Serv in between

    <xsl:variable name="test"  select ="//Itin/ServInfo[Serv[@Code='ED' and @Type = 'RED']][number(@CMD) = number($id)]/Serv/Text "  />
Aha, you seem to have come to the same conclusion...

what is the logic for that last bit? Last 8 characters, numbers after the last space?
Avatar of tesmc

ASKER

The logic would be to obtain the value after the last space in text node.
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tesmc

ASKER

thanks. it worked.