DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=17979>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=17979

Transformations using DOMSource apply templates differently than with StreamSource

           Summary: Transformations using DOMSource apply templates
                    differently than with StreamSource
           Product: XalanJ2
           Version: 2.3
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Major
          Priority: Other
         Component: Xalan
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


When transforming documents, the Xalan processor may produce different results 
when using the DOMSource vs. StreamSource.  It applies the default templates to 
non-selected elements in the DOMSource document, but properly ignores non-
selected elements (per XSLT) with the StreamSource.  Note this generally shows 
up only when embedding Xalan into another application, as the command-line code 
uses the StreamSource to load the document to be transformed.  This does not 
seem to be an issue if the DOMSource is used to load the stylesheet, only when 
the DOMSource is used to load the document to be transformed.

As an example, execute the following code:
_______________________________________________________________________________
  private static final String testDocumentString =
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + System.getProperty
("line.separator", "\n") +
  "<!--********************************************************************-->" 
+ System.getProperty("line.separator", "\n") +
  "<testRoot>" + System.getProperty("line.separator", "\n") +
  "  <testHeader>" + System.getProperty("line.separator", "\n") +
  "    <testItem>" + System.getProperty("line.separator", "\n") +
  "      This is the testItem content" + System.getProperty
("line.separator", "\n") +
  "    </testItem>" + System.getProperty("line.separator", "\n") +
  "  </testHeader>" + System.getProperty("line.separator", "\n") +
  "  <testPayload>" + System.getProperty("line.separator", "\n") +
  "    <testValue item1=\"This is Item 1\" item2=\"This is Item 2\">" + 
System.getProperty("line.separator", "\n") +
  "      This is the testValue content" + System.getProperty
("line.separator", "\n") +
  "    </testValue>" + System.getProperty("line.separator", "\n") +
  "  </testPayload>" + System.getProperty("line.separator", "\n") +
  "  <testTrailer>" + System.getProperty("line.separator", "\n") +
  "    <testItem>" + System.getProperty("line.separator", "\n") +
  "      This is the testItem content" + System.getProperty
("line.separator", "\n") +
  "    </testItem>" + System.getProperty("line.separator", "\n") +
  "  </testTrailer>" + System.getProperty("line.separator", "\n") +
  "</testRoot>" + System.getProperty("line.separator", "\n") +
  "<!--********************************************************************-->" 
+ System.getProperty("line.separator", "\n") +
  "";

  private static final String breakLine 
= "/////////////////////////////////////////////////////////////////////////////
///\n";

  private static final String testStylesheetString =
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + System.getProperty
("line.separator", "\n") +
  "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"; " + 
System.getProperty("line.separator", "\n") +
  "                exclude-result-prefixes=\"xsl\" " + System.getProperty
("line.separator", "\n") +
  "                version=\"1.0\">" + System.getProperty
("line.separator", "\n") +
  "  <xsl:output method=\"text\" indent=\"yes\" encoding=\"UTF-8\" omit-xml-
declaration=\"no\"/>" + System.getProperty("line.separator", "\n") +
  "" + System.getProperty("line.separator", "\n") +
  "  <xsl:template match=\"/\">" + System.getProperty("line.separator", "\n") +
  "    <xsl:apply-templates 
select=\"/child::testRoot/child::testPayload/child::testValue\"/>" + 
System.getProperty("line.separator", "\n") +
  "  </xsl:template>" + System.getProperty("line.separator", "\n") +
  "" + System.getProperty("line.separator", "\n") +
  "  <xsl:template match=\"testValue\">" + System.getProperty
("line.separator", "\n") +
  "    <xsl:text>" + System.getProperty("line.separator", "\n") +
  "    Item1=</xsl:text>" + System.getProperty("line.separator", "\n") +
  "    <xsl:value-of select=\"attribute::item1\" />" + System.getProperty
("line.separator", "\n") +
  "    <xsl:text>" + System.getProperty("line.separator", "\n") +
  "    Item2=</xsl:text>" + System.getProperty("line.separator", "\n") +
  "    <xsl:value-of select=\"attribute::item2\" />" + System.getProperty
("line.separator", "\n") +
  "    <xsl:text>" + System.getProperty("line.separator", "\n") +
  "    </xsl:text>" + System.getProperty("line.separator", "\n") +
  "  </xsl:template>" + System.getProperty("line.separator", "\n") +
  "" + System.getProperty("line.separator", "\n") +
  "</xsl:stylesheet>" + System.getProperty("line.separator", "\n") +
  "";

  public void testShowDOMSourceError() throws Exception
  {
    String translationResult;
    Transformer xslTransformer;
    Templates xslTemplate = null;
    Source templateSource = null;

    // Get the transformer
    templateSource = new StreamSource(new StringReader(testStylesheetString));
    xslTemplate = TransformerFactory.newInstance().newTemplates(templateSource);
    xslTransformer = xslTemplate.newTransformer();

    DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();
    xmlFactory.setExpandEntityReferences(true);
    xmlFactory.setNamespaceAware(true);
    xmlFactory.setValidating(false);
    xmlFactory.setCoalescing(true);
    xmlFactory.setIgnoringElementContentWhitespace(true);

    ByteArrayInputStream inputStream =
                         new ByteArrayInputStream(testDocumentString.getBytes
("UTF-8"));
    DocumentBuilder xmlBuilder = xmlFactory.newDocumentBuilder();
    translationResult = transformMessageToText(xslTransformer, new DOMSource
(xmlBuilder.parse(inputStream).getDocumentElement()));
    System.out.println(breakLine);
    System.out.println(translationResult);
    System.out.println(breakLine);

    translationResult = transformMessageToText(xslTransformer, new StreamSource
(new StringReader(testDocumentString)));
    System.out.println(breakLine);
    System.out.println(translationResult);
    System.out.println(breakLine);
  }

  public final String transformMessageToText(Transformer xslTransformer, Source 
documentSource) throws TransformerException, TransformerConfigurationException
  {
    StringWriter outputWriter = new StringWriter(4000);
    Result outputResult = new StreamResult(outputWriter);

    xslTransformer.transform(documentSource, outputResult);

    return outputWriter.getBuffer().toString();
  }
_______________________________________________________________________________

The following output is produced:
_______________________________________________________________________________
////////////////////////////////////////////////////////////////////////////////
  
    
      This is the testItem content
    
  
  
    
    Item1=This is Item 1
    Item2=This is Item 2
    
  
  
    
      This is the testItem content
    
  
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
    Item1=This is Item 1
    Item2=This is Item 2
    
////////////////////////////////////////////////////////////////////////////////
_______________________________________________________________________________

Note how the DOMSource run executes the default templates for the unmatched 
items prior to, and following, the one matched item.  This despite the fact 
that the root template is overridden, and only the one item of interest is 
selected in the contained apply-templates rule.  The StreamSource run correctly 
applies the defined template only to the selected item, and does not produce 
inappropriate output.
This test was run with Xalan 2.3.1.  I tried using both Xerces 1.4.4, and 
Xerces 2.3.0 for the XML parser used to build the DOM.  In both cases the 
output was the same.

Reply via email to