使用保留字符(<) in JS with XSLT

问题描述 投票:0回答:1

我需要改编一个旧的 XSLT 文件,将 XML 文件转换为 HTML/JS。已经不行了。

这是在 XSLT 文件中生成的原始 JS 代码:

function processVisibility(item, visible) {
    var childs = item.childNodes;
    for (var i = 0; i &lt; childs.length; i++) {
        if ( (childs.item(i).className == "ArchiveObject") 
            || (childs.item(i).className == "Document")
            || (childs.item(i).className == "Attachment") ) {
            if (visible) {
                childs.item(i).style.display = "block";
            } else {
                childs.item(i).style.display = "none";
            }
        }   
    }
}

字符串“<”没有转变为 < in the output HTML. As a result, JS code doesn't work in modern browsers.

如果我替换“<”与 < in the XSLT file, I get an error and that's normal. So I tried creating a variable:

<xsl:variable name="chevron">&lt;</xsl:variable>

function processVisibility(item, visible) {
    var childs = item.childNodes;
    for (var i = 0; i <xsl:value-of select="$chevron" disable-output-escaping="yes"/> childs.length; i++) {
        if ( (childs.item(i).className == "ArchiveObject") 
            || (childs.item(i).className == "Document")
            || (childs.item(i).className == "Attachment") ) {
            if (visible) {
                childs.item(i).style.display = "block";
            } else {
                childs.item(i).style.display = "none";
            }
        }   
    }
}

但是 HTML 输出不包含 < but "< ;" How can I get < to be generated in HTML using XSLT?

提前谢谢您

javascript xslt html-escape-characters
1个回答
0
投票

目前尚不清楚您的问题是什么,但一般来说,如果

xsl:output
method="html"
并且您在没有名称空间的情况下有
script
结果元素,并且 XSLT 处理器负责序列化,它将确保例如的输出

<?xml version="1.0" encoding="utf-8"?>
<html lang="en">
  <head>
    <title>XSLT fiddle test</title>
    <script>
function processVisibility(item, visible) {
    var childs = item.childNodes;
    for (var i = 0; i &lt; childs.length; i++) {
        if ( (childs.item(i).className == "ArchiveObject") 
            || (childs.item(i).className == "Document")
            || (childs.item(i).className == "Attachment") ) {
            if (visible) {
                childs.item(i).style.display = "block";
            } else {
                childs.item(i).style.display = "none";
            }
        }   
    }
}
    </script>
  </head>
  <body>
    <h1>Test</h1>
  </body>
</html>

例如

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

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

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

<!DOCTYPE html SYSTEM "about:legacy-doctype">
<html lang="en">
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-16">
    <title>XSLT fiddle test</title>
    <script>
function processVisibility(item, visible) {
    var childs = item.childNodes;
    for (var i = 0; i < childs.length; i++) {
        if ( (childs.item(i).className == "ArchiveObject") 
            || (childs.item(i).className == "Document")
            || (childs.item(i).className == "Attachment") ) {
            if (visible) {
                childs.item(i).style.display = "block";
            } else {
                childs.item(i).style.display = "none";
            }
        }   
    }
}
    </script>
  </head>
  <body>
    <h1>Test</h1>
  </body>
</html>

您可以在其中看到脚本元素包含

<
而不是
&lt;

您需要向我们提供有关您在哪个环境中使用 XSLT 的更多详细信息,提供最少但完整的 XML、XSLT、运行转换的代码、使用的 XSLT 处理器的示例,以便我们了解您为什么没有得到结果您想要以及是否可以修复它。

© www.soinside.com 2019 - 2024. All rights reserved.