XSL转换严重打印

时间:2015-04-12 14:20:40

标签: xml xslt

我有以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="university_students.css" ?>
<university>
    <students>
        <student sid="sckl9999">
            <name>Schmitt</name>
            <firstname>Klaus</firstname>
            <courses>
                <course cid="IM120">
                    <grade attempt="1" term="WS13" type="simple">4.7</grade>
                        <grade attempt="2" term="SS14" type="simple">5.0</grade>
                            <grade attempt="3" term="WS14" type="simple">1.3</grade>
                </course>
                <course cid="IM130">
                    <grade attempt="1" term="SS14" type="complex">2.1</grade>
                        <course cid="IM131">
                            <grade attempt="1" term="WS14" type="simple">2.2</grade>
                        </course>
                        <course cid="IM132">
                            <grade attempt="1" term="SS14" type="simple">2.3</grade>
                        </course>
                        <course cid="IM133">
                            <grade attempt="1" term="WS14" type="simple">2.0</grade>
                        </course>
                </course>
                <course cid="IM140">
                    <grade attempt="1" term="WS14" type="simple">1.7</grade>
                </course>
            </courses>
        </student>
        <student sid="sckl9999">
            <name>Putin</name>
            <firstname>Wladimir</firstname>
            <courses>
                <course cid="IM120">
                    <grade attempt="1" term="WS14" type="simple">1.7</grade>
                </course>
                <course cid="IM130">
                    <grade attempt="1" term="SS14" type="complex">2.3</grade>
                    <course cid="IM131">
                        <grade attempt="2" term="WS14" type="simple">2.3</grade>
                    </course>
                    <course cid="IM132">
                        <grade attempt="1" term="SS14" type="simple">2.7</grade>
                    </course>
                    <course cid="IM133">
                        <grade attempt="1" term="WS14" type="simple">2.7</grade>
                    </course>
                </course>
                <course cid="IM140">
                    <grade attempt="1" term="WS14" type="simple">1.7</grade>
                </course>
            </courses>
        </student>
        <student sid="sckl9999">
            <name>Merkel</name>
            <firstname>Angela</firstname>
            <courses>
                <course cid="IM120">
                    <grade attempt="1" term="SS14" type="simple">1.0</grade>
                </course>
                <course cid="IM130">
                    <grade attempt="1" term="SS14" type="complex">1.1</grade>
                    <course cid="IM131">
                        <grade attempt="1" term="WS14" type="simple">1.3</grade>
                    </course>
                    <course cid="IM132">
                        <grade attempt="1" term="SS14" type="simple">2.3</grade>
                    </course>
                    <course cid="IM133">
                        <grade attempt="1" term="WS14" type="simple">1.3</grade>
                    </course>
                </course>
                <course cid="IM140">
                    <grade attempt="1" term="WS14" type="simple">2.0</grade>
                </course>
            </courses>
        </student>
    </students>
</university>

以下的XSLT文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="/">
        <html>
            <body>
                <h2>Following errors were found:</h2>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="grade[@type='simple']">
            <xsl:if test="not(.='1.0' or .='1.3' or .='1.7' or .='2.0' or .='2.3' or .='2.7' or .='3.0' or .='3.3' or .='3.7' or .='4.0' or .='4.3' or .='4.7' or .='5.0')">
                <h3>
                    Simple grade <xsl:value-of select="."/> not correct in course <xsl:value-of select="../@cid" /> under term <xsl:value-of select="@term" /> at attempt <xsl:value-of select="@attempt" />.
                </h3>
            </xsl:if>
    </xsl:template>
</xsl:stylesheet>

这个XSLT应该检查XML文件中的任何等级,直到它具有类型&#34; simple&#34;然后它应该检查值是否适合if语句。我的问题是,我得到以下输出:

Following errors were found:
Schmitt Klaus 2.1
Simple grade 2.2 not correct in course IM131 under term WS14 at attempt 1.
Putin Wladimir 2.3 Merkel Angela 1.1

第三行是我需要的,但为什么还有另外的第二和第四行我真的不需要?我犯了错误,你能纠正吗?

提前谢谢。

2 个答案:

答案 0 :(得分:2)

因为你似乎关心写作&#34;认真的&#34; XSLT,你也应该

  • 始终在xsl:text内输出文字文本内容,以便始终控制将哪些字符写入输出树
  • 或许将所有允许的成绩值放在变量中并将它们存储为序列
  • 已经由leu建议,添加匹配text()的空模板以覆盖内置模板
  • 显式声明输出方法html并使用strip-space删除所有仅空白文本节点

XSLT样式表

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

    <xsl:output method="html" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <html>
            <body>
                <h2>Following errors were found:</h2>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="grade[@type='simple']">

            <xsl:variable name="allowed-grades" select="('1.0','1.3','1.7','2.0','2.3','2.7','3.0','3.3','3.7','4.0','4.3','4.7','5.0')"/>

            <xsl:if test="not(string(.) = $allowed-grades)">
                <h3>
                    <xsl:text>Simple grade </xsl:text>
                    <xsl:value-of select="."/>
                    <xsl:text> not correct in course </xsl:text>
                    <xsl:value-of select="../@cid" />
                    <xsl:text> under term </xsl:text>
                    <xsl:value-of select="@term" />
                    <xsl:text> at attempt </xsl:text>
                    <xsl:value-of select="@attempt" />
                    <xsl:text>.</xsl:text>
                </h3>
            </xsl:if>
    </xsl:template>

    <xsl:template match="text()"/>
</xsl:stylesheet>

HTML输出

<html>
   <body>
      <h2>Following errors were found:</h2>
      <h3>Simple grade 2.2 not correct in course IM131 under term WS14 at attempt 1.</h3>
   </body>
</html>

无关,但&#34;发现以下错误:&#34;在我看来,这不是一个语法英语句子。您必须使用&#34; 发现以下错误:&#34;。

答案 1 :(得分:1)

内置模板打印输入XML的文本节点。由于您的XSL没有明确的文本节点规则,因此会打印它们。

了解这一点,我们可以轻松避免您不想要的那些字符串。只需添加此模板

即可
<xsl:template match="text()"/>