【问题标题】:How to display attributes in XSLT of a specific node?如何在特定节点的 XSLT 中显示属性?
【发布时间】:2014-02-10 03:20:53
【问题描述】:

我正在尝试使用 XSLT 样式表来设置 XML 数据的样式。我的 XML 文件“V”和“H”中有两个团队用于访问和回家。我想在两个单独的表格中显示他们的统计数据。我只是不确定如何告诉 XSLT 我只想要特定团队的属性。我想能够说 xsl:value-of select="team" where the vh attribute = "V" pull出ID、Name、Record等这些值
XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?> 
<bbgame source="STAT CREW Basketball" version="4.15.03" generated="12/17/2013">
<team vh="V" id="MSU" name="MISSOURI STATE" record="8-2">
    <linescore line="24,36" score="60">
      <lineprd prd="1" score="24"></lineprd>
      <lineprd prd="2" score="36"></lineprd>
    </linescore>
</team>
<team vh="H" id="LOU" name="LOUISVILLE" record="10-1">
    <linescore line="47,43" score="90">
      <lineprd prd="1" score="47"></lineprd>
      <lineprd prd="2" score="43"></lineprd>
    </linescore>
</team>
</bbgame>

XSL:

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

<xsl:template match="bbgame">
<html>
<head>
    <link rel="stylesheet" type="text/css" href="stylesheet.css"/
</head>
<body>
    <h1>Official Basketball Box Score -- Game Totals</h1>
    <table>
        <tr>
            <td><xsl:value-of select="venue/@visname"/> vs. </td>
            <td><xsl:value-of select="venue/@homename"/></td>
            <td><xsl:value-of select="venue/@date"/></td>
            <td><xsl:value-of select="venue/@time"/></td>
            <td><xsl:value-of select="venue/@location"/></td>
        </tr>
    </table>
<table>
    <tr>
        <td>
            <xsl:value-of select="team/@id"/></td>
        <td>    <xsl:value-of select="team/linescore/@score"/></td>     
    </tr>   
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet> 

编辑:

<table>
    <xsl:if test="team[@vh="V"]">
    <tr>
        <td>    <xsl:value-of select="team/@id"/></td>
        <td>    <xsl:value-of select="team/linescore/@score"/></td>     
    </tr>
    </xsl:if>   
</table>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    您要查找的 XPath 是 /team[@vh="V]/team[@vh="H"]。 您可以在

    中使用它
    <xsl:value-of select="team[@vh='H']/@id"/>
    

    XPath 中的条件在方括号中给出。不幸的是,我不明白你想在输出中的什么地方使用它,否则我会尝试给你一个工作示例。

    一般来说,我建议您使用额外的模板来实现您想要实现的目标:

    <xsl:template match="team">
    <table>
        <tr>
            <td>
                ID: <xsl:value-of select="@id"/>
           </td>
            <td>Score:    <xsl:value-of select="linescore/@score"/></td>     
            <td>Record:    <xsl:value-of select="@record"/></td>     
        </tr>   
    </table>
    </xsl:template>
    

    然后这个模板是可重复使用的,例如像这样:

    <xsl:apply-templates select="team[@vh='H']"/>
    <xsl:apply-templates select="team[@vh='V']"/>
    

    只需从bbgame 模板的&lt;body&gt; 标记中删除您的团队表,然后将其替换为一个或多个应用模板调用。

    【讨论】:

    • 在 XSLT 的底部我有 xsl:value-of select="team/@id"。我需要在团队前面加上 / 吗?
    • 不需要 /。关键是 [@vh="..."]。
    • 好的。我很抱歉我应该更具体。我想创建一个 HTML 表,说明其中团队 vh=V,然后输出名称、记录、分数的具体值。
    • 编辑:在底部添加了其他代码部分以显示我正在寻找的内容。
    • 非常感谢您的帮助。我正在检查答案按钮。不熟悉可重用模板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多