-1

以下の xml ファイルについて言及しましたが、xml には多くの内部スタイル タグが含まれています (すべてのテキストを抽出するには、一般的な xslt コードを使用してください)。

<?xml version="1.0" encoding="UTF-8"?>
<Values>
    <Value AttributeID="11218">
        <Text>WGP03068-CNZH-00
                <style name="bold">Introduction of the Title</style>xslt
                <style name="TextStyle1">
                    The smallest font size for which kerning should be automatically adjusted.
                </style>
                <style name="bold">Reference for this book
                  <style name="TextStyle1">
                    The smallest font size for which kerning should be automatically adjusted.
                </style>
                Hope you had a good learning experience.
                </style>
                I am expecting more solution for my doughts.
            </Text>
        </Value>
    </Values>

私のXSLTコードは以下に記載されています:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <HTML>
            <xsl:apply-templates />
        </HTML>
    </xsl:template>

    <xsl:template match="Values">
        <xsl:for-each select="Value">
            <xsl:for-each select="Text">
                <p>
                <xsl:for-each select="style">
                    <xsl:value-of select="." />
                </xsl:for-each>
                </p>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

私の XSLT には、要素の開始テキストと終了テキストがなく、要素のみを読み取ります。外側と内側のタグ テキストをすべて読み取り、独自のスタイル (太字、斜体、フォント名、色など) を追加したい

私は以下のような出力を期待しています:

WGP03068-CNZH-00 タイトルの紹介xslt カーニングが自動的に調整される最小のフォント サイズ。 本書の参考文献 カーニングを自動調整するフォントの最小サイズです。私は自分の考えに対するより多くの解決策を期待しています。

4

1 に答える 1

0

より良いアプローチは、「プッシュ」アプローチを使用し、必要な html を出力する一連の一致するテンプレートとして XSLT スタイルシートをコーディングすることです。

たとえば、 XML のText要素を段落に変換するには、次のテンプレートを使用します。

  <xsl:template match="Text">
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>

スタイル要素を太字で出力するには、次のようなテンプレートを作成します。

  <xsl:template match="style[@name='bold']">
    <span style="font-weight:bold">
      <xsl:apply-templates />
      </span>
  </xsl:template>

次の XSLT を試してください

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:strip-space elements="*" />

  <xsl:template match="/">
    <HTML>
      <xsl:apply-templates />
    </HTML>
  </xsl:template>

  <xsl:template match="Text">
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>

  <xsl:template match="style[@name='bold']">
    <span style="font-weight:bold">
      <xsl:apply-templates />
      </span>
  </xsl:template>

  <xsl:template match="style">
    <span>
      <xsl:apply-templates />
    </span>
  </xsl:template>
</xsl:stylesheet> 

XML に適用すると、以下が出力されます。

<HTML>
   <p>WGP03068-CNZH-00
      <span style="font-weight:bold">Introduction of the Title</span>xslt
      <span>
         The smallest font size for which kerning should be automatically adjusted.
         </span><span style="font-weight:bold">Reference for this book
         <span>
            The smallest font size for which kerning should be automatically adjusted.
            </span>
         Hope you had a good learning experience.
         </span>
      I am expecting more solution for my doughts.

   </p>
</HTML>

この XSLT は XSLT の組み込みテンプレートを使用してテキストを出力することに注意してください。それが行われ<xsl:apply-templates />、XSLT がテキスト ノードを見つけた場合、それに一致するテンプレートがない場合は、テキストが自動的に出力されます。

于 2013-05-07T12:51:44.320 に答える