 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

April 29th, 2009, 05:03 AM
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|

April 29th, 2009, 05:27 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|

May 2nd, 2009, 02:28 AM
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

May 2nd, 2009, 07:13 AM
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|

May 6th, 2009, 12:10 PM
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.....?????
|

May 6th, 2009, 12:22 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|

May 6th, 2009, 12:23 PM
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|
 |