【问题标题】:How to use XSL to dynamically set an attribute value如何使用 XSL 动态设置属性值
【发布时间】:2013-01-21 12:36:39
【问题描述】:

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

【问题讨论】:

  • 使用 XSLT 生成具有演示意识的 HTML 会让您非常孤独。如果您希望生成用户可选择的主题,您应该使用 CSS,无论是从具有一致标记的 XSLT 生成,还是使用通过转换附加的外部文件中的预定义类。

标签: java html xml xslt


【解决方案1】:

如果没有看到您的输入格式,很难猜测,但是我认为您正在寻找属性值模板(在文字结果元素属性值中使用 { })。如果你改变了

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

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

然后familycolor 属性将通过评估这些XPath 来获取值,尽管家庭的Xpath 看起来非常可疑。上面会给出整个 msg 元素的字符串值,我怀疑应该更像/body/msg/font 来提取字体元素的字符串值。 (如果可能,通常最好避免使用text()

【讨论】:

    【解决方案2】:

    我可以建议使用这样的方法吗?如果可能,最好避免重复大部分代码(这被称为DRY principle,如果您需要更改&lt;font&gt; 部分和其中的内容,您不必在三个地方进行更改。这也可以处理既未指定 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 2013-06-11
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      相关资源
      最近更新 更多