xsl-list
[Top] [All Lists]

[xsl] URI Resolution for document('') Reference Is Broken?

2010-01-06 13:31:12
I have web application (servlet) that performs XSLT transformations and uses
a URIResolver to help resolve paths.

The web application?s 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(_dot_)xml(_dot_)transform(_dot_)stream(_dot_)StreamSource(_at_)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)




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>