Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么不是';在客户端通过XSLT在Google Chrome浏览器中正确呈现换行符(&;#10;)字符吗?_Javascript_Xml_Google Chrome_Xslt - Fatal编程技术网

Javascript 为什么不是';在客户端通过XSLT在Google Chrome浏览器中正确呈现换行符(&;#10;)字符吗?

Javascript 为什么不是';在客户端通过XSLT在Google Chrome浏览器中正确呈现换行符(&;#10;)字符吗?,javascript,xml,google-chrome,xslt,Javascript,Xml,Google Chrome,Xslt,背景: <!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="cont

背景:

<!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>
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 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>
<?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>
我创建了一个基于书店的简单网页,该网页由使用该方法的XML和XSLT(1.0)文档组合填充

我的一个XML元素(
)包含大量文本,我已经用换行符/换行符手动将这些文本分隔为段落。然而,当通过XSLT输出这个节点时,我发现这些换行符被忽略了,而节点是在一大块不间断的文本中传递的

我希望能够将这些手动文本块输出为HTML段落,并由

标记包围,因此,使用一个有用的XSLT模板,该模板使用换行符(
&&10;
)作为分隔符,并对剩余的空格应用规范化,我现在已经以我想要的方式通过了这个节点。。。差不多了

问题:

<!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>
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 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>
<?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>
在InternetExplorer和Firefox浏览器中,这个XSLT模板可以完美地工作,段落的形成和输出没有任何问题

即:

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

Firefox:

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

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

奇怪的是,这与我使用基本的XML/XSLT连接结构(没有JavaScript/客户端方法)时形成了直接对比,在这种结构中,段落在每个可用的浏览器(包括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>
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 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>
<?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>
XML:

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

哈利·波特与魔法石
J K.罗琳
1997
3.99
布卢姆斯伯里(英国)
《哈利波特与魔法石》是哈利波特系列的第一部小说,也是J.K.罗琳的处女作。
故事发生在哈利·波特之后,这位年轻的巫师在霍格沃茨魔法学校的第一年就发现了自己的魔法遗产,结交了密友和几个敌人。
在朋友们的帮助下,哈利面临着黑巫师伏地魔的卷土重来,伏地魔杀死了哈利的父母,但在哈利一岁的时候却没能杀死他。
有龙纹身的女孩
斯蒂格·拉森
2005
5.99
Norstedts Förlag(瑞典)
在瑞典斯德哥尔摩,记者Mikael Blomkvist,Millennium杂志的共同拥有者,在商人Hans-Erik Wennerström提起的诽谤案中败诉。Lisbeth Salander是一位才华横溢但麻烦重重的调查员和黑客,她为商业巨头Henrik Vanger编写了一份关于Blomkvist的广泛背景调查报告,Henrik Vanger有一项特殊任务要做。
作为交换,布洛姆克维斯特承诺提供有关温内尔斯特伦的该死的信息,他同意调查亨里克的侄女哈丽特40年前失踪并被假定谋杀的事件。
在搬进Vanger家族的大院后,Blomkvist发现了一个笔记本,里面有一个没有人能破译的名字和数字列表。
XSLT:

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

我的书店

书名:
作者:
价格: £
简介:

问题:

<!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>
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 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>
<?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模板时,我是否可以像在Firefox和IE浏览器中一样输出段落


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

如果您使用

然后我想Chrome会做你想做的,至少它在一个测试用例中为我做了。在处理属性值中的字符引用时,Chrome中似乎存在一个错误。

“大量文本,我已手动将其分成带有换行符/换行符的段落”-这就是您的错误所在。如果“段落”适合您的用例,那么将段落分成它们自己的元素。毕竟,这就是XML的意义所在。你能发布你想要创建的任何有效的HTML4或HTML5吗?嵌套的
p
元素无效,
p
元素在
span
元素中也无效。因此,任何解析器或实现都可以猜测您想要哪个呈现。XSLT创建了一个完整的HTML文档结构,而Javascript将转换结果放入
div
元素,这也不会产生有效且定义良好的文档结构。