Solved

XSL: obtain last word in a text

Posted on 2013-11-08
11
172 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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

14 Experts available now in Live!

Get 1:1 Help Now