Issue with XSL Criteria

Posted by Rachel on Stack Overflow See other posts from Stack Overflow or by Rachel
Published on 2010-05-26T15:16:46Z Indexed on 2010/05/26 21:01 UTC
Read the original article Hit count: 177

Filed under:

I am using the below piece of XSL to select the id of the text nodes whose content has a given index. This index value in input will be relative to a spcified node whose id value is known. The criteria to select the text node is,

  1. The text node content should contain a index say 'i' relative to node say 'n' whose id value i know.
  2. 'i' and 'id of n' is got as index and nodeName from the input param as seen in the xsl.

Node 'd1e5' has the text content whose index ranges from 1 to 33. When i give an index value greater than 33 i want the below criteria to fail but it does not, [sum((preceding::text(), .)[normalize-space()][. >> //*[@id=$nodeName]]/string-length(.)) ge $index]

Input xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <html xmlns="" id="d1e1">
    <head id="d1e3">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title id="d1e5">Every document must have a title</title>
    <body id="d1e9">
    <h1 id="d1e11" align="center">Very Important Heading</h1>
    <p id="d1e13">Since this is just a sample, I won't put much text here.</p>

XSL code used:

     <xsl:param name="insert-file" as="node()+">
     <insert-data><data index="1" nodeName="d1e5"></data><data index="34" nodeName="d1e5"></data></insert-data>
     <xsl:param name="nodeName" as="xsd:string" /> 
     <xsl:variable name="main-root" as="document-node()" select="/"/> 

     <xsl:variable name="insert-data" as="element(data)*"> 
     <xsl:for-each select="$insert-file/insert-data/data">
     <xsl:sort select="xsd:integer(@index)"/>
        <xsl:variable name="index" select="xsd:integer(@index)" />
        <xsl:variable name="nodeName" select="@nodeName" />
        <data text-id="{generate-id($main-root/descendant::text()[sum((preceding::text(), .)[normalize-space()][. >> //*[@id=$nodeName]]/string-length(.)) ge $index][1])}">

     <xsl:template match="/"> 
     <xsl:copy-of select="$insert-data" />

Actual output:

   <?xml version="1.0" encoding="UTF-8"?>
   <data text-id="d1t8"/>
   <data text-id="d1t14"/>

Expected output:

   <?xml version="1.0" encoding="UTF-8"?>
   <data text-id="d1t8"/>

This solution works fine if index lies between 1 and 33. Any index value greater that 33 causes incorrect text nodes to get selected. I could not understand why the text node 'd1t14' is getting selected.

Please share your thoughts.

© Stack Overflow or respective owner

Related posts about xslt

Related posts about xsl