Solved

XSL: obtain last word in a text

Posted on 2013-11-08
11
171 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
  • 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now