XLS将XML转换为HTML

时间:2013-06-10 22:55:14

标签: xml xslt

我有一个简单的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。

3 个答案:

答案 0 :(得分:2)

自从我用xsl完成任务以来已经很长时间了,但是这里......

定义模板时,它在匹配节点的上下文中操作。这意味着节点<name>是上下文。将xsl:value-of的select属性更改为.,如此

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

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

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

答案 1 :(得分: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. -->

应该给出你想要的输出。

答案 2 :(得分:0)

因为:

&#34; ...输出文件由头部标题组成,主体只有一个元素。这包含所有xml数据......&#34;

和您的评论:&#34; <ul> Guinnes Draught stout <!-- etc. --> &#34;

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

如果你有类似的话:

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

您必须将此网址添加到样式表声明中。像:

<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>
相关问题