Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8265134

Unexpected result when iterating descendants in node-set using //

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10 / RedHat OpenJDK 11.0.10 or Oracle JDK 16 2021-03-16

      A DESCRIPTION OF THE PROBLEM :
      When copying nodes from variable specified in XSLT, that tree fragment has to be converted into the node-set.

      <xsl:variable name="tree">
         <entry level="0">
            <entry level="1"/>
         </entry>
      </xsl:variable>

      However, when using XPath // shortcut for retrieving specific nodes in that tree fragment, I am getting unexpected result. This code <xsl:copy-of select="exsl:node-set($tree)//entry" /> should list two entries, but only that nested one is returned.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the test case below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      <a>
         <entry level="0">
            <entry level="1"/>
         </entry>
         <entry level="1"/>
      </a>
      <b>
         <entry level="0">
            <entry level="1"/>
         </entry>
         <entry level="1"/>
      </b>
      ACTUAL -
      <a>
          <entry level="1"/>
      </a>
      <b>
          <entry level="0">
              <entry level="1"/>
          </entry>
          <entry level="1"/>
      </b>

      ---------- BEGIN SOURCE ----------
      package my.xslt.test;

      import java.io.StringReader;
      import java.io.StringWriter;
      import javax.xml.transform.Templates;
      import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.stream.StreamResult;
      import javax.xml.transform.stream.StreamSource;

      public class IssueProvider {

          public static void main(String[] args) throws Exception {

              StringBuilder xslt = new StringBuilder();
              xslt.append("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform&#39;");
              xslt.append(" xmlns:exsl='http://exslt.org/common&#39;");
              xslt.append(" exclude-result-prefixes='exsl'");
              xslt.append(" version='1.0'>");
              xslt.append("<xsl:output indent='yes' omit-xml-declaration='yes'/>");
              xslt.append("<xsl:variable name='tree'>");
              xslt.append(" <entry level='0'>");
              xslt.append(" <entry level='1'/>");
              xslt.append(" </entry>");
              xslt.append("</xsl:variable>");
              xslt.append("<xsl:template match='/'>");
              xslt.append(" <a><xsl:copy-of select='exsl:node-set($tree)//entry'/></a>");
              xslt.append(" <b><xsl:copy-of select='exsl:node-set($tree)/descendant::entry'/></b>");
              xslt.append("</xsl:template>");
              xslt.append("</xsl:stylesheet>");

              StringWriter writer = new StringWriter();
              TransformerFactory transformerFactory = TransformerFactory.newInstance();
              Templates templates = transformerFactory.newTemplates(new StreamSource(new StringReader(xslt.toString())));
              Transformer transformer = templates.newTransformer();
              transformer.transform(new StreamSource(new StringReader("<document/>")), new StreamResult(writer));
              System.out.println(writer);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Replace //entry with /descendant::entry in XSLT

      FREQUENCY : always


            joehw Joe Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: