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
Solved

XSL: obtain last word in a text

Posted on 2013-11-08
11
177 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
How our DevOps Teams Maximize Uptime

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

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RSS Feed Enclosure URL 1 139
XML Paring  Error - Premature end of file. 7 138
XML Data Missing in PHP SimpleXML 8 71
PowerShell script to remove string in xml file 8 32
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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