?
Solved

XSL: obtain last word in a text

Posted on 2013-11-08
11
Medium Priority
?
189 Views
Last Modified: 2013-11-13
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
0
Comment
Question by:badtz7229
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39634798
<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 "  />
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39634799
for getting the last component in XSLT2
<xsl:value-of select="tokenize(normalize-space($test), ' ')[last()]"/>

in XSLT1 you will need to use recursion
0
 

Author Comment

by:badtz7229
ID: 39643010
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.
0
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 

Author Comment

by:badtz7229
ID: 39643033
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 "  />
0
 

Author Comment

by:badtz7229
ID: 39643062
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
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39643076
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 "  />
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39643082
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?
0
 

Author Comment

by:badtz7229
ID: 39643198
The logic would be to obtain the value after the last space in text node.
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 39643217
Then you need to add a recursive template

   <xsl:variable name="id">11</xsl:variable>
    <xsl:variable name="test"  select ="//Itin/ServInfo[Serv[@Code='ED' and @Type = 'RED']][number(@CMD) = number($id)]/Serv/Text "  /> 
    
    <xsl:template match="/">
        <xsl:call-template name="get-part-after-last-space">
            <xsl:with-param name="str" select="normalize-space($test)"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="get-part-after-last-space">
        <xsl:param name="str"/>
        <xsl:choose>
            <xsl:when test="contains($str, ' ')">
                <xsl:call-template name="get-part-after-last-space">
                    <xsl:with-param name="str" select="substring-after($str, ' ')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$str"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    

Open in new window

0
 

Author Closing Comment

by:badtz7229
ID: 39645029
thanks. it worked.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39645201
welcome
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question