简体   繁体   English

XSLT转换后缺少一些HTML标记

[英]Some HTML tags are missing after XSLT transformation

I'm transforming a simple XML document into HTML. 我正在将一个简单的XML文档转换为HTML。 Some of the tags such as ol and h2 are missing from the output, although they present in the template and the data itself is in the output. 输出中缺少某些标记,如olh2 ,尽管它们存在于模板中,数据本身也在输出中。 I use Saxon 9 to apply the transformation. 我使用Saxon 9来应用转换。

XML: XML:

<?xml version="1.0"?>
<recipes>
    <recipe>
        <name>Gush'gosh</name>
        <ingredients>
            <ingredient>
                <qty>1</qty>
                <unit>pound</unit>
                <food>hamburger</food>
            </ingredient>
            <ingredient>
                <qty>1</qty>
                <unit>pound</unit>
                <food>elbow macaroni</food>
            </ingredient>
            <ingredient>
                <qty>2</qty>
                <unit>cups</unit>
                <food>brown sugar</food>
            </ingredient>
            <ingredient>
                <qty>1</qty>
                <unit>bag</unit>
                <food>chopped onions</food>
            </ingredient>
            <ingredient>
                <qty>1</qty>
                <unit>teaspoon</unit>
                <food>dried dill</food>
            </ingredient>
        </ingredients>
        <instructions>
            <instruction>Brown the hamburger.</instruction>
            <instruction>Add onions and cook until transparent.</instruction>
            <instruction>Add brown sugar and dill.</instruction>
            <instruction>Cook and drain pasta.</instruction>
            <instruction>Combine meat and pasta.</instruction>
        </instructions>
    </recipe>
    <recipe>
        <name>A balanced breakfast</name>
        <ingredients>
            <ingredient>
                <qty>1</qty>
                <unit>cup</unit>
                <food>cereal</food>
            </ingredient>
            <ingredient>
                <qty>1</qty>
                <unit>glass</unit>
                <food>orange juice</food>
            </ingredient>
            <ingredient>
                <qty>1</qty>
                <unit>cup</unit>
                <food>milk</food>
            </ingredient>
            <ingredient>
                <qty>2</qty>
                <unit>slices</unit>
                <food>toast</food>
            </ingredient>
        </ingredients>
        <instructions>
            <instruction>Combine cereal and milk in bowl.</instruction>
            <instruction>Add all ingredients to table.</instruction>
        </instructions>
    </recipe>
</recipes>

XSL: XSL:

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/TR/xhtml1/strict">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>Recipes</title>
            </head>
            <body>
                <xsl:apply-templates select="/recipes/recipe"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="recipe">
        <h2>
            <xsl:value-of select="./name"/>
        </h2>
        <h3>Ingredients:</h3>
        <p>
            <!--<xsl:apply-templates select="./ingredients"/>-->
            <xsl:for-each select="./ingredients/ingredient">
                <xsl:value-of select="./qty"/><xsl:text> </xsl:text>
                <xsl:value-of select="./unit"/><xsl:text> </xsl:text>
                <xsl:value-of select="./food"/> <br/>
            </xsl:for-each>
        </p>
        <h3>Directions:</h3>
            <ol>
                <!--<xsl:apply-templates select="./instructions"/>-->
                <xsl:for-each select="./instructions/instruction">
                    <li> <xsl:value-of select="."/> </li>
                </xsl:for-each>
            </ol>
    </xsl:template>

This is simply because of this line here... 这只是因为这条线...

<xsl:template name="recipe">

This means the template is a named-templated, which can only be called with an xsl:call-template . 这意味着模板是一个名为模板的模板,只能使用xsl:call-template调用 However, you are using matching templates here, so you need to write it like this: 但是,您在此处使用匹配模板,因此您需要像这样编写它:

<xsl:template match="recipe">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM