xslt with xpath that gets information from the xml file does not work in JavaScript

advertisements

I've got an xml file. It is meant for being transformed by xslt processor. The specific template is ready, however it use some information from two additional xml files. All information is used to produce the proper output.

It works fine when I open the main xml file with my browser. The problem is: the result is just a fragment of website, it is an HTML element ready for being appended as a child. That is why I want to transform the xml with the xslt processor within a JavaScript function. Unfortunatelly, the result is half-way done. All what don't need the additional information from these two xml files imported by xpath expression is transformed well. The rest is shortly speaking absent.

JavaScript:

var xhttp = new XMLHttpRequest()
var xsltProcessor = new XSLTProcessor()
xhttp.open("GET", "contentTemplate.xsl", false)
xhttp.send()
xsltProcessor.importStylesheet(xhttp.responseXML)

function buildElement(what) {
var xmlDoc = document.implementation.createDocument("", "root", null)
xmlDoc.documentElement.appendChild(xmlDoc.createElement(what))

var resultDocumentFragment = xsltProcessor.transformToFragment(xmlDoc, document)
return resultDocumentFragment
}

xslt:

there is lots of stuff, I paste just the xslt with xpath expression

<xsl:variable name="extInfo" select="document('tagAvailableToAdd.xml')/root"/>
<xsl:for-each select="$extInfo/tag">
 <option>
  <xsl:attribute name="value"><xsl:value-of select="tagName"/></xsl:attribute>
  <xsl:value-of select="description"/>
 </option>
</xsl:for-each>

and that is just absent. Just like no information was delivered.

EDIT:

it works in firefox, doesn't in chrome. I need to improve it. Actually, I don't know how.


xslt:

there is lots of stuff, I paste just the xslt with xpath expression

<xsl:variable name="extInfo" select="document('tagAvailableToAdd.xml')/root"/>

<xsl:for-each select="$extInfo/tag">
 <option>
  <xsl:attribute name="value"><xsl:value-of

select="tagName"/>

and that is just absent. Just like no information was delivered.

Some possible reasons for this behaviour:

  1. The URI of the XML file is not the right one. This is a relative URI and this specific URI will mace the XSLT processor look for a file named 'tagAvailableToAdd.xml' and residing at the base-uri of the stylesheet. However, in this case the stylesheet is obtained dynamically and this means it doesn't have any base-uri. This is the most possible reason for the problem.

  2. The Javascript doesn't have permissions to access files in the local file system.

  3. The document() function isn't allowed by default by the XSLT processor.

  4. The text contained in the file is not a well-formed XML document.

  5. The top element of the XML file is not named root.

Solution: Specify an absolute URI as the argument to the document() function.