Mike Reddington
Mike Reddington

Reputation: 372

XSL Browser Issue

I'm able to run in firefox but not able to do it in Chrome or Safari. I want it to run in all three browsers

Chrome Version - 79.0.3945.130 (Official Build) (64-bit) Ran open -n -a "Google Chrome" --args --disable-web-security --user-data-dir=$HOME/fakeChromeDir to run xsl

Safari Version - 13.0.4 (14608.4.9.1.4) Checked Disable Local File Restrictions and Cross-Origin Restrictions

Firefox Version - 68.4.2esr (64 bit) Set to privacy.file_unique_origin;false and security.fileuri.strict_origin_policy;false

If I replace the below code in XSL

<xsl:for-each select="*">
      <xsl:apply-templates select=".[*/*]"/>
      <xsl:apply-templates
        select=".[* and not(*/*)][generate-id() = generate-id(key('table-group', concat(generate-id(..), '|', local-name()))[1])]" mode="merge-groups"/>
    </xsl:for-each>

with

<xsl:apply-templates select="*[*/*]"/>
      <xsl:apply-templates
        select="*[* and not(*/*)][generate-id() = generate-id(key('table-group', concat(generate-id(..), '|', local-name()))[1])]" mode="merge-groups"/>

It works in all browsers but I want to maintain the oder of tables as in xml. So, I'm using a for loop.

My XML file

<?xml version = '1.0' encoding = 'utf-8'?>
<?xml-stylesheet type="text/xsl" href="transformer.xsl" ?>
<group1>
    <item1>val1</item1>
    <item2>val2</item2>
    <group2>
        <item3>val3</item3>
        <item4>val4</item4>
        <group3>
            <item5>val5</item5>
        </group3>
    </group2>
    <group2>
        <item3>val6</item3>
        <item4>val7</item4>
        <group3>
            <item5>val8</item5>
        </group3>
    </group2>
    <group4>
        <item6>val9</item6>
        <item7>val10</item7>
    </group4>
    <group4>
        <item6>val11</item6>
        <item7>val12</item7>
    </group4>
</group1>

My Xsl file

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="exsl msxml"
    version="1.0">

  <xsl:output method="html" indent="yes" version="5" doctype-system="about:legacy-doctype"/>

  <xsl:key name="table-group"
    match="*[* and not(*/*)]"
    use="concat(generate-id(..), '|', local-name())"/>

  <xsl:template match="*[*]">
    <div>
      <h4><xsl:value-of select="local-name()"/></h4>
      <table>
          <thead>
              <tr>
                  <xsl:apply-templates select="*[not(*)]" mode="header"/>
              </tr>
          </thead>
          <tbody>
              <tr>
                  <xsl:apply-templates select="*[not(*)]"/>
              </tr>
          </tbody>
      </table>
    </div>
    <xsl:for-each select="*">
      <xsl:apply-templates select=".[*/*]"/>
      <xsl:apply-templates
        select=".[* and not(*/*)][generate-id() = generate-id(key('table-group', concat(generate-id(..), '|', local-name()))[1])]" mode="merge-groups"/>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="*[not(*)]" mode="header">
      <th>
          <xsl:value-of select="local-name()"/>
      </th>
  </xsl:template>

  <xsl:template match="*[not(*)]">
      <td>
          <xsl:value-of select="."/>
      </td>
  </xsl:template>

  <xsl:template match="*[*]" mode="merge-groups">
    <div>
      <h4><xsl:value-of select="local-name()"/></h4>
      <table>
          <thead>
              <tr>
                  <xsl:apply-templates select="*[not(*)]" mode="header"/>
              </tr>
          </thead>
          <tbody>
              <xsl:apply-templates select="key('table-group', concat(generate-id(..), '|', local-name()))" mode="row"/>
          </tbody>
      </table>
    </div>
  </xsl:template>

  <xsl:template match="*" mode="row">
      <tr>
          <xsl:apply-templates select="*"/>
      </tr>
  </xsl:template>

  <xsl:template match="/">
    <html>
      <head>
        <link rel="stylesheet" href="style.css" type="text/css" />
        <title>Diagnostic Report</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

Upvotes: 2

Views: 243

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167401

I think XPath 1 has a restriction that a predicate cannot be applied to the abbreviated form . but you should be able to replace it by self::node() so use e.g. <xsl:apply-templates select="self::node()[*/*]"/>. Make the same change for other place where you try to use .[predicate].

Upvotes: 4

Related Questions