xml - XSL 浏览器问题

标签 xml google-chrome xslt browser safari

我可以在 Firefox 中运行,但无法在 Chrome 或 Safari 中运行。我希望它在所有三个浏览器中运行

Chrome 版本 - 79.0.3945.130(官方版本)(64 位) 运行 open -n -a "Google Chrome"--args --disable-web-security --user-data-dir=$HOME/fakeChromeDir 来运行 xsl

Safari 版本 - 13.0.4 (14608.4.9.1.4) 选中禁用本地文件限制和跨源限制

Firefox 版本 - 68.4.2esr(64 位) 设置为privacy.file_unique_origin;false和security.fileuri.strict_origin_policy;false

如果我替换 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>

<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"/>

它适用于所有浏览器,但我想保持 xml 中表的顺序。因此,我使用了 for 循环。

我的 XML 文件

<?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>

我的 Xsl 文件

<?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>

最佳答案

我认为 XPath 1 有一个限制,即谓词不能应用于缩写形式 .但你应该可以将其替换为 self::node()所以使用例如<xsl:apply-templates select="self::node()[*/*]"/> 。对您尝试使用 .[predicate] 的其他地方进行相同的更改.

关于xml - XSL 浏览器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59857493/

相关文章:

包含非空字符串和元素的混合内容复杂类型的 XML 架构

javascript - window.onbeforeunload 没有返回任何值的任何不良副作用?

javascript - 我的Chrome扩展程序无法加载,直到我再次重新加载/刷新相同的youtube视频页面

xml - XPath表达式fn:max()无法识别

xml - 如何在 XSLT 1.0 中迭代 IDREFS 值?

python - XML 到 CSV 格式

java - 使用过滤器解析 XML

c# - 使用正则表达式从 XML 字符串中删除 XML 节点命名空间前缀

javascript - Chrome 扩展程序中的功能无法运行

java - 在 Wildfly 8.1 中设置 javax.xml.transform.TransformerFactory