Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 29th, 2009, 05:03 AM
Authorized User
 
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default remove the first digit in xml

Hi everybody,

in the below xml code, the element <first-page>635</first-page> and <last-page>645</last-page> having the same digit i.e 3 digit, 2 digit, and 4 digit etc my <last-page> first number should be remove in the out xml.

Is there any possibility in the xsl...

input xml:

Code:
<host>
<issue>
<series>
<title>
<maintitle>Cognition</maintitle>
</title>
<volume-nr>37</volume-nr>
</series>
<issue-nr>2</issue-nr>
<date>1975</date>
</issue>
<pages>
<first-page>635</first-page>
<last-page>647</last-page>
</pages>
</host>
Need Output xml:

<host>
<issue>
<series>
<title>
<maintitle>Cognition</maintitle>
</title>
<volume-nr>37</volume-nr>
</series>
<issue-nr>2</issue-nr>
<date>1975</date>
</issue>
<pages>
<first-page>635</first-page>
<last-page>47</last-page>
</pages>
</host>
Reply With Quote
  #2 (permalink)  
Old April 29th, 2009, 05:27 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't think there's any magic answer to this one. It needs a recursive function something like this, in pseudo-code:

function name="abbreviated-end"
param name=start
param name=end
if (string-length($start) = string-length($end)) and number($end) > number($start) and substring($start,1,1) = substring($end,1,1)
then abbreviated-end(substring($start,2), substring($end,2))
else $second

The actual coding of this will vary depending whether you use XSLT 1.0 or 2.0, but the logic is the same. You may need to handle some special cases, e.g. where the start and end are the same (627-627).
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old May 2nd, 2009, 02:28 AM
Authorized User
 
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for your reply michael,

One more request i'm not familiar in xslt, here we using xslt 2.0

Please give us the below requirement sample codes.....................

Thanks,
Nagaraj
Reply With Quote
  #4 (permalink)  
Old May 2nd, 2009, 07:13 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an attempt to translate the pseudo code into some XSLT 2.0 stylesheet:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/2009/mf"
  exclude-result-prefixes="mf"
  version="2.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:function name="mf:abbreviated-end" as="xs:string">
    <xsl:param name="start" as="xs:string"/>
    <xsl:param name="end" as="xs:string"/>
    <xsl:sequence
      select="if (string-length($start) eq string-length($end)
                  and number($end) gt number($start)
                  and substring($start, 1, 1) = substring($end, 1, 1))
              then mf:abbreviated-end(substring($start, 2), substring($end, 2))
              else $end"/>
  </xsl:function>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="last-page">
    <xsl:copy>
      <xsl:sequence select="mf:abbreviated-end(preceding-sibling::first-page[1], .)"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #5 (permalink)  
Old May 6th, 2009, 12:10 PM
Authorized User
 
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Fantastic!! Thanks a lot....

One more questions:

If the <first-page>S45</first-page> and <last-page>S65</last-page>, need to remove the string also "S" your xslt working for matching the number and removed the number in output xml....

Is there any method.....?????
Reply With Quote
  #6 (permalink)  
Old May 6th, 2009, 12:22 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You could change the calls on number() in "number($start) gt number($end)" to calls on mf:numeric-part() defined as

<xsl:function name="mf:numeric-part" as="xs:integer">
<xsl:param name="in" as="xs:string"/>
<xsl:sequence select="xs:integer(replace($in, '[^0-9]', ''))"/>
</xsl:function>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #7 (permalink)  
Old May 6th, 2009, 12:23 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

It is not clear what you want to achieve now. Do you simply want to strip the character 'S' at the start of first-page and last-page? You could do that in XSLT 2.0 using the replace function http://www.w3.org/TR/xpath-functions/#func-replace e.g.
Code:
<xsl:template match="first-page">
  <xsl:copy>
    <xsl:value-of select="replace(., '^S', '')"/>
  </xsl:copy>
</xsl:template>
If you know there is always a leading 'S' then obviously substring(., 2) would do.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove numbers in XML files using xsl srkumar XSLT 1 April 15th, 2008 06:43 AM
To remove a XML node in VB.Net jkusmanto XML 4 May 23rd, 2006 09:18 AM
7 Digit number jemacc SQL Server 2000 3 April 4th, 2004 01:02 AM
Add and Remove Nodes in XML document. tutul128 XML 3 March 1st, 2004 10:17 AM



All times are GMT -4. The time now is 02:06 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.