[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

URI Resolution for document('') Reference Is Broken?

Subject: URI Resolution for document('') Reference Is Broken?
From: "Roger L. Cauvin" <roger@xxxxxxxxxx>
Date: Wed, 6 Jan 2010 13:30:38 -0600
 URI Resolution for document('') Reference Is Broken?
I have web application (servlet) that performs XSLT transformations and uses
a URIResolver to help resolve paths.

The web applications directory structure is as follows:

war
  data
  META-INF
  WEB-INF

The 'data' directory contains the following files:

Months.xml
GetMonthLengths.xsl
date.get-days-in-month.template

Months.xml contains:

<?xml version="1.0"?>
<months>
  <month>1</month>
  <month>2</month>
  <month>3</month>
</months>

GetMonthLengths.xsl contains:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">
  <xsl:import href="/data/date.get-days-in-month.template.xsl"/>
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <xsl:element name="month-lengths">
      <xsl:apply-templates select="months/month"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="month">
    <xsl:element name="month-length">
      <xsl:call-template name="date:get-days-in-month">
        <xsl:with-param name="month" select="."/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

date.get-days-in-month.template contains:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl=http://www.w3.org/1999/XSL/Transform
                xmlns:date=http://exslt.org/dates-and-times
                extension-element-prefixes="date">

<date:month-lengths>
   <date:month>31</date:month>
   <date:month>28</date:month>
   <date:month>31</date:month>
   <date:month>30</date:month>
   <date:month>31</date:month>
   <date:month>30</date:month>
   <date:month>31</date:month>
   <date:month>31</date:month>
   <date:month>30</date:month>
   <date:month>31</date:month>
   <date:month>30</date:month>
   <date:month>31</date:month>
</date:month-lengths>

<xsl:template name="date:get-days-in-month">
   <xsl:param name="month"/>
   <xsl:variable name="month-days"
select="document('')/*/date:month-lengths/date:month" />
   <xsl:value-of select="$month-days[number($month)]" />
</xsl:template>

</xsl:stylesheet>

My URIResolver is as follows:

  public Source resolve(
    String href,
    String basePath)
    {
    Source source = null;

    System.out.println("href: " + href);
    System.out.println("basePath: " + basePath);

    if (href != null)
      {
      boolean isEmptyHRef = href.equals("");
      if (!isEmptyHRef)
        {
        ServletContext servletContext = getServletContext();
        InputStream inputStream = servletContext.getResourceAsStream(href);
        if (inputStream != null)
          source = new StreamSource(inputStream);
        }
      }

    System.out.println("source: " + source);

    return source;
    }

The output is:

href: /data/date.get-days-in-month.template.xsl
basePath:
source: javax.xml.transform.stream.StreamSource@1d439fe
href:
basePath:
source: null
Error on line 1 column 1 of file:/c:/morandum/source/http/servlet/war/:
  SXXP0003: Error reported by XML parser: Content is not allowed in prolog.
outputText: <month-lengths>
   <month-length/>
   <month-length/>
Recoverable error
   <month-length/>
  FODC0002: org.xml.sax.SAXParseException: Content is not allowed in prolog.
</month-lengths>
Recoverable error
  FODC0005: Document has been marked not available:
Recoverable error
  FODC0005: Document has been marked not available:

As you can see, URI resolution works fine for resolving <xsl:import
href="/data/date.get-days-in-month.template.xsl"/>).  But it does NOT work
properly (or according to spec, as far as I can tell) for resolving
<xsl:variable name="month-days"
select="document('')/*/date:month-lengths/date:month" />.

This problem occurs whether I use Resin or Google App Engine as my web
server, and whether I use Saxon 9.2 or the default XML parser for
transformation.  Things work flawlessly if I run the transformation using an
ordinary Java program from the command line (without a URIResolver).

My understanding is that, if the URIResolver returns null, resolution will
be handled in the default manner.  And the default manner for document()
is to refer to the current document.  It is not doing so.

Who is to blame here?  The servlet containers, the XML parsers, or me?

--
Roger L. Cauvin
@rcauvin (Twitter)
cauvin.blogspot.com (blog)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.