2015-04-12 88 views
0

我有下面的XML文件:严重打印出来XSL转换的

<?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> 

而且follwoing 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文件中的任何年级,直到它具有“简单”类型,然后应检查该值是否适合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

既然你似乎是担心写“严重”的XSLT,你也应该

  • 为了永远是其中人物的控制内xsl:text总输出文字文本内容被写入到输出树
  • 也许把所有允许的等级值的变量,并将它们存储为一个序列
  • 由列伊已经建议,加空模板匹配text()到overrid E中的内置模板
  • 明确规定的输出方法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> 

不相关,但在我看来,“发现以下错误:”不是一个语法上的英语句子。您必须使用“发现以下错误:“。

1

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

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

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