如何使用XSL动态设置属性值

Yar*_*usa 0 html java xml xslt

我是 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>
Run Code Online (Sandbox Code Playgroud)

Dav*_*sle 5

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

 <font family="/body/msg[font/text()]" color="/body/msg/color">
Run Code Online (Sandbox Code Playgroud)

 <font family="{/body/msg[font/text()]}" color="{/body/msg/color}">
Run Code Online (Sandbox Code Playgroud)

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