gpt4 book ai didi

javascript - 为什么换行符 () 通过客户端上的 XSLT 在 Google Chrome 浏览器中正确呈现字符?

转载 作者:行者123 更新时间:2023-11-30 16:30:52 25 4
gpt4 key购买 nike

背景:

我创建了一个基于书店的简单网页,该网页由使用 on the client via Javascript 的 XML 和XSLT (1.0) 文档组合填充。方法。

我的一个 XML 元素 ( <synopsis></synopsis> ) 包含大量文本,我已将这些文本手动分成带有换行符/回车符的段落。然而,当通过 XSLT 输出这个节点时,我发现这些换行符被忽略了,而是以一大块不间断的文本来传递节点。

我希望能够将这些手动文本 block 中的每一个输出为 HTML 段落,并用 <p></p> 包围标签,所以,利用一个有用的 XSLT 模板,它使用换行符 ( &#10; ) 作为分隔符并对剩余的空白应用规范化,我现在已经设法按照我想要的方式传递这个节点。 .差不多

问题:

在 Internet Explorer 和 Firefox 浏览器中,此 XSLT 模板完美运行,段落的形成和输出没有任何问题。

IE:

IE Screenshot

火狐:

Firefox Screenshot

但在 Google Chrome 浏览器中,我假设 &#10;当在客户端专门使用 XSLT 方法/设置时,(换行)字符以不同的方式处理或解释?我这样说是因为不是在每个换行符之后形成段落,而是在每个单词之后创建段落!

Chrome Screenshot

奇怪的是,这与我使用基本的 XML/XSLT 连接结构(没有 JavaScript/On the Client 方法)时形成鲜明对比,其中段落在每个可用的浏览器(包括 Google Chrome)中都正确输出! 奇怪

设置:

这是我的 HTML/JS/XML/XSLT 设置,因此人们可以重现我的确切问题。

HTML:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Book Store</title>
<script type="text/javascript" src="books.js"></script>
</head>

<body>

<div id="content">
</div>

</body>
</html>

JavaScript:

function loadXMLDoc(filename) {
if (window.ActiveXObject)
{
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
else
{
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", filename, false);
try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}

function displayResult(xml,xsl)
{
xml = loadXMLDoc("books.xml");
xsl = loadXMLDoc("books.xsl");
// code for IE
if (window.ActiveXObject || xhttp.responseType == "msxml-document")
{
ex = xml.transformNode(xsl);
document.getElementById("content").innerHTML = ex;
}
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml, document);
document.getElementById("content").appendChild(resultDocument);
}
}

window.onload=function() {

displayResult('books.xml','books.xsl');
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title>Harry Potter and the Philosopher's Stone</title>
<author>J K. Rowling</author>
<year>1997</year>
<price>3.99</price>
<publisher>Bloomsbury (UK)</publisher>
<synopsis>
Harry Potter and the Philosopher's Stone is the first novel in the Harry Potter series and J. K. Rowling's debut novel.

The plot follows Harry Potter, a young wizard who discovers his magical heritage as he makes close friends and a few enemies in his first year at the Hogwarts School of Witchcraft and Wizardry.

With the help of his friends, Harry faces an attempted comeback by the dark wizard Lord Voldemort, who killed Harry's parents, but failed to kill Harry when he was just a year old.
</synopsis>
</book>
<book>
<title>The Girl with the Dragon Tattoo</title>
<author>Stieg Larsson</author>
<year>2005</year>
<price>5.99</price>
<publisher>Norstedts Förlag (SWE)</publisher>
<synopsis>
In Stockholm, Sweden, journalist Mikael Blomkvist, co-owner of Millennium magazine, has lost a libel case brought against him by businessman Hans-Erik Wennerström. Lisbeth Salander, a brilliant but troubled investigator and hacker, compiles an extensive background check on Blomkvist for business magnate Henrik Vanger, who has a special task for him.

In exchange for the promise of damning information about Wennerström, Blomkvist agrees to investigate the disappearance and assumed murder of Henrik's grandniece, Harriet, 40 years ago.

After moving to the Vanger family's compound, Blomkvist uncovers a notebook containing a list of names and numbers that no one has been able to decipher.
</synopsis>
</book>
</bookstore>

XSLT:

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

<xsl:template match="/">
<html>
<body>
<h2>My Bookstore</h2>
<xsl:apply-templates/>

</body>
</html>
</xsl:template>

<xsl:template match="book">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="author"/>
<xsl:apply-templates select="price"/>
<xsl:apply-templates select="synopsis"/>
</p>
</xsl:template>

<xsl:template match="title">
Book Title: <span style="color:#000000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>

<xsl:template match="author">
Author: <span style="color:#000000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>

<xsl:template match="price">
Price: <span style="color:#000000">
&#163;<xsl:value-of select="."/></span>
<br />
</xsl:template>

<xsl:template match="synopsis">
Synopsis: <span style="color:#38A930">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</span>
<br/>
</xsl:template>

<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'&#10;'"/>
<xsl:variable name="token" select="normalize-space(substring-before(concat($text, $delimiter), $delimiter))" />
<xsl:if test="$token">
<p>
<xsl:value-of select="$token"/>
</p>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

问题:

因此,有人知道解决这个非常小众的浏览器相关问题的解决方案吗? Google Chrome 可以使用任何进一步的十六进制代码来处理我可以尝试的换行符吗?当使用 在客户端 方法设置我的 XSLT 模板时,我能否以与我在 Chrome 中的 Firefox 和 IE 中所做的相同的方式输出段落?

任何有关这方面的帮助或建议都将受到热烈欢迎。非常感谢!

最佳答案

如果您使用 <xsl:param name="delimiter" xml:space="preserve">&#10;</xsl:param> 设置参数的默认值那么我认为 Chrome 会做你想做的事,至少它在 http://home.arcor.de/martin.honnen/xslt/test2015102604.html 的测试用例中对我有用。 .在处理属性值中的字符引用时,Chrome 中似乎存在错误。

关于javascript - 为什么换行符 () 通过客户端上的 XSLT 在 Google Chrome 浏览器中正确呈现字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33343060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com