首页 文章

格式化XSLT的XML标记

提问于
浏览
-1

我'm attempting to style an XML file using XSLT. I'使用了一个BASH shell脚本,该脚本使用WGET命令每5秒从VLC Media Player下载一次XML文件 . 下面是XML文件的示例部分,其中包含I 'd like to style with XSLT. Notice in the example XML, there are numerous tags that start with <info name=' unique '> and ends with </info>. For example, the first tag is <info name=' date '>1991</info> and the third tag is <info name=' filename'> Something in the Way </ info> .

<category name="meta">
<info name='date'>1991</info>
<info name='artwork_url'>file://D:/Music/Nirvana/Nevermind/Folder.jpg</info>
<info name='filename'>Something in the Way</info>  </category>

我在XML文件的头部使用此代码来设置XML的样式 .

<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>

当我在浏览器中显示.xml文件时,它通过XSLT设置样式 . 但是,仅显示第一个<info>标记 . 在此示例中,仅显示<info name ='date'> 1991 </ info> . 我的总体目标是只在浏览器中显示带名和歌名 .

我实际上有我认为是一个非常不寻常的解决方案,它将使用Linux中的 SEDAWK 命令与某种类型的auto_increment选项一起查找和替换文本信息,以便将XML转换为类似于下面 . 然后我可以设置XSLT来读取info1,info2,info3等等 .

<info1>data</info1>
<info2>data</info2>
<info3>data</info3>

很难完成这种不寻常的解决方法,因为VLC输出动态信息标签,因此一个XML文件可能具有<info name ='date'>标签,而下一个XML文件可能不包含<info name ='date'>标签,所以我不能使用期望$ 27的AWK命令始终是<info name ='date'> . 这似乎也不是这个问题的推荐解决方案,并且可能是一个易变的方法,这就是为什么我在这里发布问题以获得反馈 .

我也对XML命名空间进行了一些研究,但我不太确定我是否应该专注于命名空间 . 任何见解,提示或建议将非常感谢 .

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
</tr>
<xsl:for-each select="category">
<tr>
<td><xsl:value-of select="info"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

1 回答

  • 1

    在XSLT 1.0中, xsl:value-of 获取所选节点集的第一个节点的值 . 如果你想获得所有这些,请使用以下内容:

    <xsl:for-each select="category">
        <tr>
            <xsl:for-each select="info">
                <td><xsl:value-of select="."/></td>
            </xsl:for-each>
        </tr>
    </xsl:for-each>
    

    或者,您可以通过 name 属性单独选择它们,例如:

    <xsl:for-each select="category">
        <tr>
            <td><xsl:value-of select="info[@name='date']"/></td>
            <td><xsl:value-of select="info[@name='filename']"/></td>
        </tr>
    </xsl:for-each>
    

    或按其职位:

    <xsl:for-each select="category">
        <tr>
            <td><xsl:value-of select="info[1]"/></td>
            <td><xsl:value-of select="info[3]"/></td>
        </tr>
    </xsl:for-each>
    

相关问题