Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XPath

From: "Marrow" <m--a-r-r-o-w@--------------------.--->
To: NULL
Date: 11/3/2004 10:55:00 AM
Hi,

Wow, and you want that in one XPath expression?  Assuming the current
context node (or text node thereof) is the sentence and that $break is the
desired position (e.g. 300) something like...

concat(substring(.,1,$break),
           substring-before(substring(.,$break+1,string-length(.) *
not(substring(.,$break,1) = '.') ),'.'),
           substring(.,$break+1,string-length(.) *
not(contains(substring(.,$break+1),'.'))),
           substring('.',1,1 * (not(substring(.,$break,1) = '.') or
not(contains(.,'.')))))


Heh, it ain't pretty but it works (i think???)! ;)

Some test code...

XML
<root>
  <!-- 
1234567890123456789012345678901234567890123456789012345678901234567890123456
7890123456 -->
  <sentence>dshjsd sjhhjds sdjsj sj. sdjhh sjsjd sjjhsdh. sjsjhd sjjshhd
sjhdsjh sjhjd.</sentence>
  <sentence>kds skj swiuui weiuuiew ewiuiew ewiuwieuiiuwe weiiuewui
ewiiuewiu weiiuewi</sentence>
  <sentence>weoio ksjdkj skdskjjk woewoi weoiew weiooiew ew. wepoopew
ewpopoew weoppoewo</sentence>
  <sentence>kjdkjds sdkjweiu iwuew wiewiu. oweio ewowpwe wiiwue woiwie.
wppwepoew wiuwei.</sentence>
</root>

XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="break" select="30"/>

<xsl:template match="/">
  <!-- list original and spliced strings... -->
  <xsl:text>       123456789012345678901234567890&#10;</xsl:text>
  <xsl:for-each select="root/sentence">
    <xsl:text>old:- "</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>"&#10;new:- "</xsl:text>
    <xsl:value-of select="concat(substring(.,1,$break),

substring-before(substring(.,$break+1,string-length(.) *
not(substring(.,$break,1) = '.') ),'.'),
                                 substring(.,$break+1,string-length(.) *
not(contains(substring(.,$break+1),'.'))),
                                 substring('.',1,1 *
(not(substring(.,$break,1) = '.') or not(contains(.,'.')))))"/>
    <xsl:text>"&#10;&#10;</xsl:text>
  </xsl:for-each>
  <xsl:text>&#10;&#10;</xsl:text>
  <!-- find the maximum length of those... -->
  <xsl:variable name="max-length">
    <xsl:for-each select="root/sentence">
      <xsl:sort select="string-length(concat(substring(.,1,$break),

substring-before(substring(.,$break+1,string-length(.) *
not(substring(.,$break,1) = '.') ),'.'),
                                      substring(.,$break+1,string-length(.)
* not(contains(substring(.,$break+1),'.'))),
                                      substring('.',1,1 *
(not(substring(.,$break,1) = '.') or not(contains(.,'.'))))))"
data-type="number" order="descending"/>
      <xsl:if test="position() = 1">
        <xsl:value-of select="string-length(concat(substring(.,1,$break),

substring-before(substring(.,$break+1,string-length(.) *
not(substring(.,$break,1) = '.') ),'.'),

substring(.,$break+1,string-length(.) *
not(contains(substring(.,$break+1),'.'))),
                                            substring('.',1,1 *
(not(substring(.,$break,1) = '.') or not(contains(.,'.'))))))"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
  <xsl:text>The longest string is </xsl:text>
  <xsl:value-of select="$max-length"/>
  <xsl:text> chars long</xsl:text>
</xsl:template>
</xsl:stylesheet>


Cheers
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator



"js" <anonymous@d...> wrote in message
news:e1ldXqTwEHA.4048@T......
> Hi,
>
> I am trying to use XPath to extract substring of a text node.  The
> substring should be at least 300 characters long. If the 300th character
> is in the middle of a sentence, the substring should include the
> complete sentence which the 300th character resides.  I use XPath
> function substring($outputContent,1,$displayLength).  What function or
> XPath syntax will allow me to find the period of the ending sentence so
> that I can dynamically adjust the $displayLength variable?  Thanks.
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent