如何获取独特的价值节点XML [英] How Get distinct value node XML

查看:64
本文介绍了如何获取独特的价值节点XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触XML,希望对您有所帮助。
我有以下XML:

I am new in XML, so I hope your help. I have the following 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. 以及每门课程学习的学生人数。


推荐答案


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

I need know two things: 1.all courses

使用

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




2。以及如何

2.And how many students studing in each course.

对于给定的课程使用

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

其中 $ vCourseId 必须用实际的课程ID代替。

where $vCourseId must be substituted with the actual course 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

这篇关于如何获取独特的价值节点XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆