Search code examples
xslt

json-doc returns blank page in a stylesheet when opened in browser


When I use the json-doc in a browser within XSLT for read XML and produces HTML browsers (Firefox, Chrome and Safari) return blank page.

the part of XSLT is like this:

<td>
  <xsl:variable name="activityLink" select="json-doc(concat('https://deims.org/api/activities/', substring-after(//cs:campaign, 'https://deims.org/activity/')))"/>
    <a href="{//cs:campaign}" target="_blank">
      <xsl:value-of select="$activityLink?title"/>
    </a>
</td>

the section about the cs:campaign is: <cs:campaign>https://deims.org/activity/50d7a52d-e384-4ed4-9976-5bf9c8302843</cs:campaign>

I aspecting the title of this: <td>Lago di Garda - Italy ...</td>


Solution

  • With SaxonJS 2 this works:

    const xml = `<sample>https://deims.org/api/activities/50d7a52d-e384-4ed4-9976-5bf9c8302843</sample>`;
    
    const xslt = `<?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="#all"
      expand-text="yes">
      
      <xsl:template match="sample">
        <xsl:copy>{json-doc(.)?title}</xsl:copy>
      </xsl:template>
    
      <xsl:mode on-no-match="shallow-copy"/>
    
      <xsl:template match="/" name="xsl:initial-template">
        <xsl:next-match/>
        <xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} {system-property('Q{http://saxon.sf.net/}platform')}</xsl:comment>
      </xsl:template>
      
    </xsl:stylesheet>
    `;
    
    const result = SaxonJS.XPath.evaluate(`
      transform(map {
        'stylesheet-text' : $xslt,
        'source-node' : parse-xml($xml),
        'delivery-format' : 'serialized'
      })?output`,
      null,
      { params : { xml : xml, xslt : xslt } }
    );
    
    console.log(result);
    <script src="https://martin-honnen.github.io/SaxonJS-2.6/SaxonJS2.js"></script>