簡體   English   中英

如何使用XSL動態設置屬性值

[英]How to use XSL to dynamically set an attribute value

我是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>

很難看到沒有輸入格式,但是我想您正在尋找屬性值模板(在文字結果元素屬性值中使用{ } )。 如果你改變

 <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來提取font元素的字符串值。 (如果可能,通常最好避免使用text()

我可以建議使用這種方法嗎? 在可能的情況下,最好避免重復代碼的大部分(這被稱為DRY原理 ,對於以下內容,如果需要更改該<font>部分及其中的內容,則不要不必在三個地方進行更改。這也可以處理未指定bolditalic情況,而您目前尚未考慮:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM