4

我是 XML 新手,希望对您有所帮助。我有以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
    <Students> -<Student Id="001">
        <Name>Peter</Name>
        <LastName>Kohen</LastName> -<Courses> -<Course Id="01">
                <Name>C#</Name>
            </Course> -<Course Id="02">
                <Name>Java</Name>
            </Course>
        </Courses>
    </Student> -<Student Id="002">
        <Name>Nick</Name>
        <LastName>Nikes</LastName> -<Courses> -<Course Id="02">
                <Name>Java</Name>
            </Course> -<Course Id="03">
                <Name>Oracle</Name>
            </Course>
        </Courses>
    </Student> -<Student Id="003">
        <Name>Rafi</Name>
        <LastName>rafifa</LastName> -<Courses> -<Course Id="02">
                <Name>Java</Name>
            </Course> -<Course Id="03">
                <Name>Oracle</Name>
            </Course>
        </Courses>
    </Student> -<Student Id="004">
        <Name>Yosi</Name>
        <LastName>Koen</LastName> -<Courses> -<Course Id="04">
                <Name>SQL</Name>
            </Course> -<Course Id="03">
                <Name>Oracle</Name>
            </Course>
        </Courses>
    </Student>
</Students>

我需要知道两件事:

  1. 所有课程
  2. 以及每门课程有多少学生学习。
4

2 回答 2

3

XPath 1.0
课程:

//Course[not(preceding::Course/@Id = @Id)]
于 2012-11-12T10:47:05.737 回答
3

我需要知道两件事:1.所有课程

使用

/*/*/Courses/Course[not(Name = preceding::Course/Name)]

2.每门课程有多少学生。

对于给定的课程使用

count(/*/Student[Courses/Course/@Id = $vCourseId])

where$vCourseId必须替换为实际的课程 ID。

基于 XSLT 的验证

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "/*/*/Courses/Course[not(Name = preceding::Course/Name)]"/>
     ===============
     <xsl:for-each select=
     "/*/*/Courses/Course[not(Name = preceding::Course/Name)]">

     <xsl:variable name="vCourseId" select="@Id"/>

     Course Id = <xsl:value-of select="@Id"/>, Students = <xsl:text/>
     <xsl:value-of select="count(/*/Student[Courses/Course/@Id = $vCourseId])"/>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<Students> -
    <Student Id="001">
        <Name>Peter</Name>
        <LastName>Kohen</LastName> -
        <Courses> -
            <Course Id="01">
                <Name>C#</Name>
            </Course> -
            <Course Id="02">
                <Name>Java</Name>
            </Course>
        </Courses>
    </Student> -
    <Student Id="002">
        <Name>Nick</Name>
        <LastName>Nikes</LastName> -
        <Courses> -
            <Course Id="02">
                <Name>Java</Name>
            </Course> -
            <Course Id="03">
                <Name>Oracle</Name>
            </Course>
        </Courses>
    </Student> -
    <Student Id="003">
        <Name>Rafi</Name>
        <LastName>rafifa</LastName> -
        <Courses> -
            <Course Id="02">
                <Name>Java</Name>
            </Course> -
            <Course Id="03">
                <Name>Oracle</Name>
            </Course>
        </Courses>
    </Student> -
    <Student Id="004">
        <Name>Yosi</Name>
        <LastName>Koen</LastName> -
        <Courses> -
            <Course Id="04">
                <Name>SQL</Name>
            </Course> -
            <Course Id="03">
                <Name>Oracle</Name>
            </Course>
        </Courses>
    </Student>
</Students>

评估两个 XPath 表达式(每门课程重复第二个),并将这些评估的结果复制到输出

<Course Id="01">
   <Name>C#</Name>
</Course>
<Course Id="02">
   <Name>Java</Name>
</Course>
<Course Id="03">
   <Name>Oracle</Name>
</Course>
<Course Id="04">
   <Name>SQL</Name>
</Course>
     ===============


     Course Id = 01, Students = 1

     Course Id = 02, Students = 3

     Course Id = 03, Students = 3

     Course Id = 04, Students = 1
于 2012-11-12T13:29:25.457 回答