1

我有一个简单的 xml 文件,我需要将其转换为包含一些嵌套的无序列表的 html 这是 xml:

<Beer>
    <name>Guinnes Draught</name>
    <type>stout</type>
    <manufacturer>Arthur Guinness Son &amp; Co</manufacturer>
    <ingredients>
        <ingredient>malt</ingredient>
        <ingredient>hop</ingredient>
        <ingredient>water</ingredient>
        <ingredient>barley</ingredient>
        <ingredient>yeast</ingredient>
    </ingredients>
    <alcChars>
        <alcohol>4.2</alcohol>
        <density>15</density>
        <calories>150</calories>
        <filtered>true</filtered>
        <package>glass bottle</package>
        <volume>0.33</volume>
    </alcChars>
</Beer>

这是我的 xsl 文件(已编辑):

<xsl:template match="/">
    <html>
        <head>
            <title>Beer description</title>
        </head>
        <body>
            <div>Beer:</div>
            <ul>
                <xsl:apply-templates />
            </ul>
        </body>
    </html>
</xsl:template>

<xsl:template match="name">
    <li>Name - <xsl:value-of select="." /> </li>
</xsl:template>

<xsl:template match="type">
    <li>Type - <xsl:value-of select="." /> </li>
</xsl:template>

<xsl:template match="manufacturer">
    <li>Manufacturer - <xsl:value-of select="." /> </li>
</xsl:template>

<xsl:template match="ingredients">
    <li>Ingredients: <br/>
        <ul>
            <xsl:for-each select="ingredient">
                <li> <xsl:value-of select="." /> </li>
            </xsl:for-each>
        </ul>
    </li>
</xsl:template>

<xsl:template match="alcChars | nonAlcChars" >
    <li>Characteristics: <br/>
        <ul>
            <xsl:for-each select="*">
                <li> <xsl:value-of select="." /> </li>
            </xsl:for-each>
        </ul>
    </li>
</xsl:template>

显然这段代码有问题,因为输出文件由带有标题的头部和只有一个<ul>元素的主体组成。这<ul>包含所有 xml 数据,但它甚至没有包含在<li>. 我对 xslt 很陌生,如果有任何建议可以帮助我获得类似的东西,我将不胜感激

<ul>
    <li> Name - Guinness </li>
    <li> Type - Stout </li>
    <li> Chars: <br/>
        <ul>
            <li> Alc - 4.5 </li>
            <li> Dens - 8 </li>
            <!-- etc. -->

从我的xml。

4

3 回答 3

2

自从我用 xsl 做任何事情以来已经有很长时间了,但是这里......

定义模板时,它在匹配节点的上下文中运行。这意味着节点<name>是上下文。把 xsl:value-of 的 select 属性改成.这样

<xsl:template match="name">
    <li>Name - <xsl:value-of select="." /> </li>
</xsl:template>

您也希望对所有其他模板执行此操作。

如果我完全错了,请原谅我。我可能有点生疏了。

于 2013-06-10T23:24:57.393 回答
1

除了 Marc 的回答,您还需要更改:

        <xsl:for-each select="ingredients">
            <li> <xsl:value-of select="ingredient" /> </li>
        </xsl:for-each>

至:

        <xsl:for-each select="ingredient">
            <li> <xsl:value-of select="." /> </li>
        </xsl:for-each>

通常,如果你使用类似的东西会更好:

<xsl:template match="ingredients">
    <li>Ingredients: <br/>
        <ul><xsl:apply-templates select="ingredient"/></ul>
    </li>
</xsl:template>

<xsl:template match="ingredients">
    <li><xsl:value-of select="."/></li>
</xsl:template>

然后,这允许您执行以下操作:

<xsl:template match="alcChars | nonAlcChars" >
    <li>Characteristics: <br/>
        <ul><xsl:apply-templates select="*"/></ul>
    </li>
</xsl:template>

<xsl:template match="density">
    <li>Density - <xsl:value-of select="."/></li>
</xsl:template>

<!-- etc. -->

这应该给你想要的输出。

于 2013-06-10T23:44:21.143 回答
0

因为:

“...输出文件由带有标题的头部和只有一个元素的主体组成。这包含所有的 xml 数据......”

和你的评论:“ <ul> Guinnes Draught stout <!-- etc. -->

我假设一个命名空间问题。
可能是您在简化输入 XML 的同时隐藏了默认命名空间声明。如果您的 XML 中有默认命名空间,则必须将此命名空间与前缀添加到您的 xslt 并在模板规则中使用此前缀。

如果你有类似的东西:

<Beer xmlns="http://www.beer.org" >

您必须将此 url 添加到您的样式表声明中。喜欢:

<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" 
            xmlns:b="http://www.beer.orgl"
            exclude-result-prefixes="b"
            version="1.0">

而不是使用这个命名空间前缀,如:

    <xsl:template match="b:name">
        <li>
            Name - <xsl:value-of select="." />
        </li>
    </xsl:template>
于 2013-06-11T08:05:12.230 回答