0

私はxsltを初めて使用し、チャットアプリケーションを実行しています。ユーザーのセッションを、ユーザーが事前定義した色とフォントで表示されるxmlファイルとして保存したいので、xsltを使用してそれを実現しましたが、方法がわかりません。 xmlからフォントを取得し、それをhtmlタグに適用して、ユーザーが選択したフォントで表示されるようにします。

<xsl:choose>
    <xsl:when test="/body/msg[italic/text()='true']">
        <i>
            <font family="/body/msg[font/text()] color="/body/msg/color">
                <xsl:value-of select="from" /><p>: </p>
                <xsl:value-of select="content"/><br/>
            </font>
        </i>
    </xsl:when>
    <xsl:when test="/body/msg[bold/text()='true']">
        <b>
            <font family="/body/msg[font/text()]" color="/body/msg/color">
                <xsl:value-of select="from" /><p>: </p>
                <xsl:value-of select="content"/><br/>
            </font>
        </b>
    </xsl:when>
    <xsl:when test="/body/msg[bold/text()='true'] and /body/msg[italic/text()='true']">
        <b>
            <i>
                <font family="/body/msg[font/text()]" color="/body/msg/color">
                    <xsl:value-of select="from" /><p>: </p>
                    <xsl:value-of select="content"/><br/>
                </font>
            </i>
        </b>
    </xsl:when> 
</xsl:choose>
4

2 に答える 2

4

入力形式を見ずに推測するのは難しいですが、属性値テンプレートを探していると思います ({ }リテラルの結果要素の属性値を使用しています)。変えたら

 <font family="/body/msg[font/text()]" color="/body/msg/color">

 <font family="{/body/msg[font/text()]}" color="{/body/msg/color}">

次に、これらの XPath を評価することによって属性familycolor属性が値を取得しますが、ファミリの Xpath は非常に疑わしいように見えます。上記は msg 要素全体の文字列値を与えるでしょう/body/msg/font 。フォント要素の文字列値を抽出するのにもっと似ているはずです。text()(可能であれば、通常は使用を避けるのが最善です)

于 2013-01-19T22:56:15.097 に答える
0

このようなアプローチを使用することを提案できますか? 可能であれば、コードの大部分を繰り返さないようにすることをお勧めします (これはDRY 原則<font>として知られています。次のように、その部分とその中のものを変更する必要がある場合は、これは、どちらboldも指定されていない場合も処理しitalicますが、あなたのものは現在考慮されていません:

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

  <xsl:template match="body">
    <body>
      <xsl:apply-templates select="msg" />
    </body>
  </xsl:template>

  <xsl:template match="msg">
    <xsl:param name="formatting" select="bold | italic"/>

    <xsl:choose>
      <xsl:when test="$formatting[self::bold and . = 'true']">
        <b>
          <xsl:apply-templates select=".">
            <xsl:with-param name="formatting" select="$formatting[not(self::bold)]" />
          </xsl:apply-templates>
        </b>
      </xsl:when>
      <xsl:when test="$formatting[self::italic and . = 'true']">
        <i>
          <xsl:apply-templates select=".">
            <xsl:with-param name="formatting" select="$formatting[not(self::italic)]" />
          </xsl:apply-templates>
        </i>
      </xsl:when>
      <xsl:otherwise>
        <font family="{font}" color="{color}">
          <xsl:value-of select="from" />
          <p>: </p>
          <xsl:value-of select="content"/>
          <br/>
        </font>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

この入力で実行すると:

<root>
  <body>
    <msg>
      <bold>true</bold>
      <italic>false</italic>
      <font>Arial</font>
      <color>Blue</color>
      <from>Shelly</from>
      <content>Hey, how are you doing?</content>
    </msg>
    <msg>
      <bold>false</bold>
      <italic>true</italic>
      <font>Times New Roman</font>
      <color>Red</color>
      <from>Tom</from>
      <content>What's up?</content>
    </msg>
    <msg>
      <bold>false</bold>
      <italic>false</italic>
      <font>Courier</font>
      <color>Yellow</color>
      <from>Fred</from>
      <content>I've been trying to reach you</content>
    </msg>
    <msg>
      <bold>true</bold>
      <italic>true</italic>
      <font>Comic Sans</font>
      <color>Green</color>
      <from>Lisa</from>
      <content>Talk to you later.</content>
    </msg>
  </body>
</root>

プロデュース:

<body>
  <b>
    <font family="Arial" color="Blue">
      Shelly<p>: </p>Hey, how are you doing?<br />
    </font>
  </b>
  <i>
    <font family="Times New Roman" color="Red">
      Tom<p>: </p>What's up?<br />
    </font>
  </i>
  <font family="Courier" color="Yellow">
    Fred<p>: </p>I've been trying to reach you<br />
  </font>
  <b>
    <i>
      <font family="Comic Sans" color="Green">
        Lisa<p>: </p>Talk to you later.<br />
      </font>
    </i>
  </b>
</body>
于 2013-01-20T06:11:36.897 に答える