[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]
RE: [xml-dev] from xpath to surrounding context?
- From: "Evan Owens" <Evan.Owens@portico.org>
- To: "Mukul Gandhi" <gandhi.mukul@gmail.com>
- Date: Thu, 8 Nov 2007 12:01:25 -0500
Thanks! We need to output the markup as well as the text (for
diagnostic purposes) but this will get us started in the right
direction.
-----Original Message-----
From: Mukul Gandhi [mailto:gandhi.mukul@gmail.com]
Sent: Thursday, November 08, 2007 12:27 AM
To: Evan Owens
Cc: xml-dev@lists.xml.org
Subject: Re: [xml-dev] from xpath to surrounding context?
Here is a custom utility for this requirement using XSLT 2.0, and Saxon.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/"
xmlns:fn="http://custom-functions"
version="2.0">
<xsl:output method="text" />
<xsl:variable name="xpath" select="'xpath expression ...'" />
<xsl:template match="/">
<xsl:variable name="beforeText"
select="fn:beforeNow(saxon:evaluate($xpath), 40)" />
<xsl:variable name="afterText"
select="fn:afterNow(saxon:evaluate($xpath), 40)" />
<xsl:value-of select="$beforeText" />
<xsl:text>
</xsl:text>
<xsl:value-of select="$xpath" />
<xsl:text>
</xsl:text>
<xsl:value-of select="$afterText" />
</xsl:template>
<xsl:function name="fn:beforeNow" as="xs:string">
<xsl:param name="items" as="item()*" />
<xsl:param name="x" as="xs:integer" />
<xsl:variable name="length"
select="string-length(string-join($items/preceding::text(),''))" />
<xsl:variable name="result"
select="substring(string-join($items/preceding::text(),''), $length -
$x, $length)" />
<xsl:sequence select="$result" />
</xsl:function>
<xsl:function name="fn:afterNow" as="xs:string">
<xsl:param name="items" as="item()*" />
<xsl:param name="x" as="xs:integer" />
<xsl:variable name="length"
select="string-length(string-join($items/following::text(),''))" />
<xsl:variable name="result"
select="substring(string-join($items/following::text(),''), 0, $x)" />
<xsl:sequence select="$result" />
</xsl:function>
</xsl:stylesheet>
Hope you could adapt this in your environment.
On Nov 7, 2007 7:09 PM, Evan Owens <Evan.Owens@portico.org> wrote:
>
> Given an xpath expression that points into a document, can anyone
> suggest a clever way to extract an arbitrary chunk of the document
> immediately before and after that point? Say, 40 characters before
and after?
>
> Here's the use case: we have an error report that is generated as part
> of an XSLT batch transformation process. The error message include
> the xpath that describes the element that caused the problem; e.g.,
> /ART[1]/BM[1]/BIBL[1]/BIB[5]/NOTE[1]/P[1]. The users of the report
> have requested that we add a bit of context to help them assess the
> errors without having to open every single file...a preview snippet as
it were.
>
> It doesn't have to be a pure XML solution nor does it have to be
> extremely efficient in processor time. These are not large documents
> and it would be okay to reread the files as part of the error report
generation.
>
> Thanks in advance for any suggestions as to ways to tackle this
problem.
>
> Evan Owens
--
Regards,
Mukul Gandhi
[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]