This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Checking node position


"David W. Black" wrote:
> 
> Hi all,
> 
> I essentially have an undifferentiated list of elements (old html) that I need to transform to xml, and the only hooks I have are certain textual consistencies:
> 
> <p>textxtxtxt</p>
> <p>head1</p>
> <p>text_associated_semantically_with_head1</p>
> <p>text_associated_semantically_with_head1</p>
> <p>head2</p>
> 
> etc.
> 
> Thus I want to itentify the position of the <p> with text="head1" (ie. 2), the position of <p> with text="head2" (ie.5) so that I can then wrap all <p>s between 2 and 5 with an element in the result tree.
> 
> My problem is what XPath expression returns the 'correct' position?
> //p[text()=head1] returns "1" (1st occurrence of p with text = head1), as opposed to "2" the position of p which _incidentally_ is head1 amongst all <p>s.
> 
> I can, I suppose, pre-process using generate-id() to ascribe and id attribute to each <p> and then do my transformations.... but I'd really like to ties this up in one stylesheet, not two?
> 
> Any ideas, anyone??

This approach is a little more table-driven:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:x="foo" exclude-result-prefixes="x">

<x:keynode>head1</x:keynode>
<x:keynode>head2</x:keynode>

<xsl:key name="parent" match="p"
  use="generate-id(preceding-sibling::p[string(.) =
document('')//x:keynode/text()])" />

<xsl:variable name="head-types"
select="document('')//x:keynode/text()"/>

<xsl:template match="p[. = $head-types]">
  <xsl:element name="{.}">
    <xsl:apply-templates select="key('parent', generate-id())[not(. =
$head-types)]"
      mode="inner"/>
  </xsl:element>
</xsl:template>

<xsl:template match="p"/>

<xsl:template match="p" mode="inner">
  <sub>
    <xsl:copy-of select="."/>
  </sub>
</xsl:template>

</xsl:stylesheet>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]