Martin Honnen created XALANJ-2799:
-------------------------------------

             Summary: java.lang.ClassCastException: 
org.apache.xpath.objects.XString cannot be cast to java.lang.String when using 
XSLT key function on the right side of step operator /
                 Key: XALANJ-2799
                 URL: https://issues.apache.org/jira/browse/XALANJ-2799
             Project: XalanJ2
          Issue Type: Bug
      Security Level: No security risk; visible to anyone (Ordinary problems in 
Xalan projects.  Anybody can view the issue.)
          Components: transformation, Xalan, Xalan-CmdLine
    Affects Versions: The Latest Development Code
         Environment: Java 8, Windows 11
            Reporter: Martin Honnen
         Attachments: cars.xml, key-fn-call-in-last-step-test1.xsl

Working with the latest  XSLT 3.0 branch of Apache Xalan, I get an exception 
`java.lang.ClassCastException: org.apache.xpath.objects.XString cannot be cast 
to java.lang.String` when trying to use the XSLT `key` function on the right 
side of the "path" operator `/` e.g. `key('car-by-color', 'red') / 
key('lot-for-car', @vin)`.

I would expect the right side of the / to be evaluated for each node selected 
by the expression on the left side of /, without giving any errors. This is an 
established XPath 2 and XSLT 2 coding pattern.

The full code

{code:xml}
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  exclude-result-prefixes="xs">

  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="car-by-color" match="cars/car" use="@color"/>
     
  <xsl:key name="lot-for-car" match="lot" use="car/@vin"/>
   
  <xsl:template match="lot">
      <lot id="{@id}"/>
  </xsl:template>
  
  <xsl:template match="/">
      <set>
          <problem>
              <question>Which lots have red cars?</question>
              <answer>
                  <xsl:apply-templates select="key('car-by-color', 'red') / 
key('lot-for-car', @vin)"/>
              </answer>
          </problem>
      </set>
  </xsl:template>

</xsl:stylesheet>
{code} 

Sample XML input is e.g.

{code:xml}
<dir>
    <cars>
        <car vin="101" color="red"/>
        <car vin="102" color="green"/>
        <car vin="103" color="red"/>
        <car vin="104" color="red"/>
    </cars>
    <lots>
        <lot id="L">
            <car vin="101"/>
            <car vin="103"/>
        </lot>
        <lot id="M">
            <car vin="102"/>
        </lot>
        <lot id="N">
            <car vin="104"/>
        </lot>
    </lots>
</dir>
{code}

 Full error output:
{code}
(Position des Fehlers unbekannt)java.lang.ClassCastException: 
org.apache.xpath.objects.XString cannot be cast to java.lang.String

(Position des Fehlers unbekannt)XSLT-Fehler 
(javax.xml.transform.TransformerException): Keine Verarbeitungsanweisung f³r 
xml-stylesheet gefunden in 
..\..\OneDrive\Documents\xslt\blog-xslt-3-by-example\xalan-xslt3-tests\cars.xml.
Exception in thread "main" java.lang.RuntimeException: Keine 
Verarbeitungsanweisung für xml-stylesheet gefunden in 
..\..\OneDrive\Documents\xslt\blog-xslt-3-by-example\xalan-xslt3-tests\cars.xml.
        at org.apache.xalan.xslt.Process.doExit(Process.java:1176)
        at org.apache.xalan.xslt.Process.main(Process.java:1149)
{code}




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@xalan.apache.org
For additional commands, e-mail: dev-h...@xalan.apache.org

Reply via email to