Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


[xsl] User-defined function for linenumber

From: "Jesper Tverskov" <jesper@----------->
To:
Date: 8/1/2007 8:11:00 AM
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


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