XML,可能是我无法找到的简单错误(考试XML)

时间:2016-08-04 15:02:58

标签: xml xslt xpath xsd

因此,我们在几个小时后盯着xPath错误进行了关于XML的考试(我失败了),我来这里寻求帮助。

    

<!-- TODO customize transformation rules 
     syntax recommendation http://www.w3.org/TR/xslt 
-->
<xsl:template match="/">
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="StylesheetWorkoutWimVanoverwalle.css" />
            <title>Workouts by Wim Van Overwalle and friends</title>
        </head>
        <body>
            <h1>Workouts by Wim Van Overwalle and Friends</h1>
            <p>Total Number of workouts By Wim Van Overwalle</p>
            <div>
            <xsl:value-of select ="sum(workout)"/>
            <xsl:for-each select="workouts/workout/@type='running'">
                <xsl:sort select="workout/starttime"
                          order = "ascending"
                          data-type="number"/>
                <xsl:value-of select = "naam"/>
                <xsl:value-of select="distance/@km"/>
                <xsl:value-of select="starttime/@date"/>
                <xsl:value-of select="location/@country"/>
                <xsl:value-of select="distance/@km"/>
                <xsl:value-of select="duration"/>                                      
            </xsl:for-each>
            </div>
            <xsl:for-each select="workouts/workout[@type='cycling']">
                <xsl:value-of select = "naam"/>
                <xsl:value-of select="distance/@km"/>
                <xsl:value-of select="starttime/@date"/>
                <xsl:value-of select="location/@country"/>
                <xsl:value-of select="distance/@km"/>
                <xsl:value-of select="duration"/>                                      
            </xsl:for-each>
             <xsl:for-each select="workouts/workout[@type='running']">
                  <xsl:value-of select = "naam"/>
                <xsl:value-of select="distance/@km"/>
                <xsl:value-of select="starttime/@date"/>
                <xsl:value-of select="location/@country"/>
                <xsl:value-of select="distance/@km"/>
                <xsl:value-of select="duration"/> 
             </xsl:for-each>
        </body>
    </html>
</xsl:template>

^ XSL文件

<xs:schema version="1.0"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       elementFormDefault="qualified">
<xs:element name = "workouts">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref= "workout"
                        minOccurs="1"
                        maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name= "workout">
    <xs:complexType>
        <xs:sequence>
            <xs:element name = "person"/>
            <xs:element name = "starttime" type="xs:dateTime"/>
            <xs:element name = "distance" type="xs:decimal"/>
            <xs:element name= "duration" type="xs:time"/>
            <xs:element name="avgpace" type="xs:time"/>
            <xs:element name = "location">
                <xs:complexType>
                <xs:attribute name="country">
                    <xs:simpleType>
                        <xs:restriction base= "xs:string">
                            <xs:pattern value="[A-Z][A-Z]"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name= "type">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value='running'/>
                    <xs:enumeration value='cycling'/>
                    <xs:enumeration value='walking'/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:element>

^ xsd file

workouts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="WorkoutsWimVanoverWalle.xsd">
    <workout type="running">
        <person>Wim Van Overwalle</person>
        <starttime>2016-05-11T07:36:00</starttime>
        <distance unit="km">6.09</distance>
        <duration>00:36:50</duration>
        <avgpace>00:05:52</avgpace>
        <location country="BE">Kortrijk</location>
    </workout>

^我的xml文件的一部分

得到一个错误,上面写着:&#34; xslt-transformation期间的错误:预期NodeSet是xPath-Expression&#34;请帮助:/

1 个答案:

答案 0 :(得分:0)

当我运行xsltproc时,收到以下错误消息:

runtime error: file foo.xsl line 17 element for-each
The 'select' expression does not evaluate to a node set.

有问题的一行是:

 <xsl:for-each select="workouts/workout/@type='running'">

也许你的意思是这样说:

 <xsl:for-each select="workouts/workout[@type='running']">