Uploaded image for project: 'XalanJ2'
  1. XalanJ2
  2. XALANJ-2602

Selected attribute is not iterated correctly

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.7.1, 2.7.2
    • None
    • transformation, XPath, XSLTC
    • Security Level: No security risk; visible to anyone (Ordinary problems in Xalan projects. Anybody can view the issue.)
    • None
    • Android 5 and Windows 10 x64

    Description

      items.xml
      <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
      <?xml-stylesheet type="text/xsl" href="items.xslt"?>
      <root>
        <item name="Item 1">
          <item name="Item 2">
            <item name="Item 3">
              <item name="Item 4" />
            </item>
          </item>
        </item>
      </root>
      

      The context where the problem occurs is "Item 4"'s element node.

      items.xslt
      <?xml version="1.0" encoding="utf-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      	<xsl:output method="html" encoding="utf-8" />
      	<xsl:template match="//item[@name='Item 4']">
      		<p>ancestor::item<ul>
      		<xsl:for-each select="ancestor::item">
      			<li>
      				<xsl:value-of select="position()" />
      				/
      				<xsl:value-of select="last()" />
      				:
      				<xsl:value-of select="@name" />
      			</li>
      		</xsl:for-each>
      		</ul></p>
      
      		<p>ancestor::item/@name<ul>
      		<xsl:for-each select="ancestor::item/@name">
      			<li>
      				<xsl:value-of select="position()" />
      				/
      				<xsl:value-of select="last()" />
      				:
      				<xsl:value-of select="." />
      			</li>
      		</xsl:for-each>
      		</ul></p>
      	</xsl:template>
      </xsl:stylesheet>
      

      If the parents of the context node are selected, everything is ok, but if the {{@name}}s of each parent is selected, then the iteration stops abruptly without error.

      Expected output (sans HTML)
      ancestor::item
      1 / 3 : Item 1
      2 / 3 : Item 2
      3 / 3 : Item 3
      
      ancestor::item/@name
      1 / 3 : Item 1
      2 / 3 : Item 2
      3 / 3 : Item 3
      
      Actual output (sans HTML)
      ancestor::item
      1 / 3 : Item 1
      2 / 3 : Item 2
      3 / 3 : Item 3
      
      ancestor::item/@name
      1 / 3 : Item 1
      

      I tried Java 5 and 8 built-in processor, Chrome 52 and Internet Explorer 11; they all agree that the second iterations should result in 3 items. Originally I found the issue while running a similar XPath query foreach in Android 5.0 on a Galaxy S5, which uses Xalan 2.7.1; on my Windows 10 machine I ran the processing with Xalan 2.7.2 on the classpath:

      import java.io.*;
      import javax.xml.transform.*;
      import javax.xml.transform.stream.*;
      
      public class Solution {
      	public static void main(String[] args) throws IOException, TransformerException {
      		TransformerFactory factory = TransformerFactory.newInstance();
      		Transformer transformer = factory.newTransformer(new StreamSource(new FileInputStream("items.xslt")));
      		StringWriter capture = new StringWriter();
      		transformer.transform(new StreamSource(new FileInputStream("items.xml")), new StreamResult(capture));
      		System.out.println(capture.toString());
      	}
      }
      

      Attachments

        1. Solution.java
          0.6 kB
          Robert Papp
        2. items.xslt
          0.7 kB
          Robert Papp
        3. items.xml
          0.3 kB
          Robert Papp

        Activity

          People

            shathaway Steven J. Hathaway
            twisterrob Robert Papp
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: