1

次のような XML があります。

<bunch of other things>
<bunch of other things>
<errorLog> error1 \n error2 \n error3 </errorLog>

errors1この XML が通過する XSLT を変更して、 throughの後に改行を適用したいと考えていますerror3

errorLog の出力または XSLT ファイルの内容を完全に制御できますが、出力 HTML に改行を表示させるために XML または XSLT を作成する方法がわかりません。XML 出力を改行の原因となる特殊文字に変更する方が簡単ですか、それとも XSLT を\n改行として解釈するように変更する必要がありますか?

このサイトには、私が望むものに似たものを含む例がありますが、私のXSLT<errorLog>は別のテンプレートにネストされており、テンプレート内のテンプレートがどのように機能するかわかりません。

4

2 に答える 2

3

バックスラッシュは、C や Java などの多くの言語でエスケープ文字として使用されますが、XML や XSLT では使用されません。スタイルシートに \n を入れると、それは改行ではなく、2 文字のバックスラッシュの後に "n" が続きます。改行を記述する XML の方法は&#xa;. ただし、改行を HTML でブラウザーに送信すると、スペースとして表示されます。ブラウザーに改行を表示させたい場合は、<br/>要素を送信する必要があります。

于 2013-05-23T21:18:44.897 に答える
2

errorLog要素を制御できる場合は、そこでリテラル LF 文字を使用することもできます。XSLT に関する限り、他の文字と何ら変わりはありません。

改行で表示される HTML を作成する<br/>場合は、XML ソースにあるマーカーの代わりに要素を追加する必要があります。このように、各エラーを個別の要素内に配置できれば、最も簡単です。

<errorLog>
  <error>error1</error>
  <error>error2</error>
  <error>error3</error>
</errorLog>

そうすれば、XSLT は、テキスト自体を分割するというかなり厄介なプロセスを経る必要がなくなります。

あなたの質問から取得したこのXMLデータを使用して

<document>
  <bunch-of-other-things/>
  <bunch-of-other-things/>
  <errorLog>error1 \n error2 \n error3</errorLog>
</document>

このスタイルシート

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

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

  <xsl:template match="/document">
    <html>
      <head>
        <title>Error Log</title>
      </head>
      <body>
        <xsl:apply-templates select="*"/>
      </body>
    </html>
  </xsl:template>

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

  <xsl:template match="errorLog">
    <p>
      <xsl:call-template name="split-on-newline">
        <xsl:with-param name="string" select="."/>
      </xsl:call-template>
    </p>
  </xsl:template>

  <xsl:template name="split-on-newline">
    <xsl:param name="string"/>
    <xsl:choose>
      <xsl:when test="contains($string, '\n')">
        <xsl:value-of select="substring-before($string, '\n')"/>
        <br/>
        <xsl:call-template name="split-on-newline">
          <xsl:with-param name="string" select="substring-after($string, '\n')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$string"/>
        <br/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

この出力が生成されます

<html>
   <head>
      <title>Error Log</title>
   </head>
   <body>
      <bunch-of-other-things/>
      <bunch-of-other-things/>
      <p>error1 <br/> error2 <br/> error3<br/>
      </p>
   </body>
</html>
于 2013-05-23T19:44:35.247 に答える