[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

User-defined function for linenumber

Subject: User-defined function for linenumber
From: "Jesper Tverskov" <jesper@xxxxxxxxxxx>
Date: Wed, 1 Aug 2007 10:11:00 +0200
 User-defined function for linenumber
Hi list

I am trying to make a user-defined function that can return the
linenumber of a node (yes I know Saxon has an extension function doing
the same). So far my solution works for element nodes and that is good
enough for now.

But I am using the analyze-string element. I would like to find a
solution not using analyze-string in order to get a solution that
would also work when the expressions are modified and transferred to
Schematron. I am not sure if it is possible? Some clever REGEX?

If the document does not contain the element node in question also as
text inside comments, CDATA sections and PI's, I can do without
analyze-string. I use analyze-string only to neutralize false
positives simply by deleting all "&lt;" found inside comments, CDATA
sections and PIs.

It is possible to do without analyze-string under all circumstances?

My function works like this:

I load the document as unparsed text and deletes all "&lt;" from
comments, CDATA sections and PIs to avoid false positives. I then use
the node name (e.g.: "p") to split the string and make a new string of
the items until the node number (e.g.: the third "p"). I then count
the characters, delete all linefeeds, count again, and subtract to get
the count of linefeeds until the element node in question.

My function looks like this:

<xsl:function name="please:linenumber">
        <xsl:param name="document-uri"/><!-- similar to document-uri() -->
        <xsl:param name="node-name"/><!-- e.g.: 'p' -->
        <xsl:param name="node-number"/><!-- e.g.: '3', that is the
third p -->
        <xsl:variable name="unparsed" select="unparsed-text($document-uri)"/>
        <xsl:variable name="unparsed2">
            <xsl:analyze-string select="$unparsed"
regex="&lt;!--.*?--&gt;|&lt;!\[CDATA\[.*?\]\]&gt;|&lt;\?.*?\?&gt;"
flags="s">
            <xsl:matching-substring>
                <xsl:value-of select="replace(., '&lt;', '')"/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
        </xsl:variable>
       <xsl:value-of
select="string-length(string-join(subsequence(tokenize($unparsed2,
concat('&lt;', $node-name)), 1, $node-number), ' ')) -
            string-length(replace(string-join(subsequence(tokenize($unparsed2,
concat('&lt;', $node-name)), 1, $node-number), ' '), '&#xA;', ''))"/>
    </xsl:function>

Cheers
Jesper Tverskov
http://www.xmlplease.com

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.