为什么我的 xml 查询不起作用
why is my xml Query not working
我需要从 XML 文件中查询(我在此处包含)
Link towards the xml file
我需要找到所有具有 'Eric'
教授开设的课程的先决条件的课程
预期查询结果:
- 编程抽象
- 计算机组织和系统
- 计算机科学家概率论导论
- 数字系统 II
我试着按步骤工作
我知道我需要找到所有具有先决条件 CS106A 和 CS106B 的课程
所以我尝试了
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Course_Catalog>
<xsl:copy-of select="//Course[Prerequisites/Prereq = 'CS106B' and 'CS106A']/Title"/>
</Course_Catalog>
</xsl:template>
</xsl:stylesheet>
但这给了我
- 计算机组织和系统
- 计算机科学家概率论导论
- 数字系统 II
如果我尝试
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Course_Catalog>
<xsl:copy-of select="//Course[Prerequisites/Prereq = 'CS106A' and 'CS106B']/Title"/>
</Course_Catalog>
</xsl:template>
</xsl:stylesheet>
我只得到
- 编程抽象
所以我可以假设我的 "and" 没有工作?
编辑,重新查找教授以名字为先决条件的课程
您可以使用 xsl:key
创建索引查找,并将其用作模板中的谓词:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="ProfLookup"
match="//Course"
use="@Number"/>
<xsl:template match="/">
<CoursesWithPrerequsiteOfEric>
<xsl:apply-templates select="//Course[key('ProfLookup',
Prerequisites/Prereq)/Instructors/Professor/First_Name='Eric']"/>
</CoursesWithPrerequsiteOfEric>
</xsl:template>
<xsl:template match="Course">
<Course>
<xsl:value-of select="Title"/>
</Course>
</xsl:template>
</xsl:stylesheet>
其中吐出:
<CoursesWithPrerequsiteOfEric>
<Course>Programming Abstractions</Course>
<Course>Computer Organization and Systems</Course>
<Course>Introduction to Probability for Computer Scientists</Course>
<Course>Digital Systems II</Course>
</CoursesWithPrerequsiteOfEric>
编辑
很抱歉没有首先了解原始要求。您的中间步骤当然是找到所有以 'CS106A' 或 'CS106B' 作为先决条件的课程。但是显然上面的key lookup解决了实际需求
<xsl:template match="/">
<CoursesWithPrerequsiteOfEric>
<xsl:apply-templates select="//Course[Prerequisites/Prereq = 'CS106A' or
Prerequisites/Prereq = 'CS106B']"/>
</CoursesWithPrerequsiteOfEric>
</xsl:template>
回复:更多要求
好吧,您可以内联密钥,但它变得更难理解,IMO:
select="//Course[Prerequisites/Prereq = //Course[Instructors/Professor/First_Name='Eric']/@Number]"
然后重新计算 - 您需要复习一下 xsl functions。
<xsl:value-of select="count(//Course[Prerequisites/Prereq = //Course[Instructors/Professor/First_Name='Eric']/@Number])"/>
我需要从 XML 文件中查询(我在此处包含)
Link towards the xml file
我需要找到所有具有 'Eric'
预期查询结果:
- 编程抽象
- 计算机组织和系统
- 计算机科学家概率论导论
- 数字系统 II
我试着按步骤工作
我知道我需要找到所有具有先决条件 CS106A 和 CS106B 的课程
所以我尝试了
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Course_Catalog>
<xsl:copy-of select="//Course[Prerequisites/Prereq = 'CS106B' and 'CS106A']/Title"/>
</Course_Catalog>
</xsl:template>
</xsl:stylesheet>
但这给了我
- 计算机组织和系统
- 计算机科学家概率论导论
- 数字系统 II
如果我尝试
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Course_Catalog>
<xsl:copy-of select="//Course[Prerequisites/Prereq = 'CS106A' and 'CS106B']/Title"/>
</Course_Catalog>
</xsl:template>
</xsl:stylesheet>
我只得到
- 编程抽象
所以我可以假设我的 "and" 没有工作?
编辑,重新查找教授以名字为先决条件的课程
您可以使用 xsl:key
创建索引查找,并将其用作模板中的谓词:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="ProfLookup"
match="//Course"
use="@Number"/>
<xsl:template match="/">
<CoursesWithPrerequsiteOfEric>
<xsl:apply-templates select="//Course[key('ProfLookup',
Prerequisites/Prereq)/Instructors/Professor/First_Name='Eric']"/>
</CoursesWithPrerequsiteOfEric>
</xsl:template>
<xsl:template match="Course">
<Course>
<xsl:value-of select="Title"/>
</Course>
</xsl:template>
</xsl:stylesheet>
其中吐出:
<CoursesWithPrerequsiteOfEric>
<Course>Programming Abstractions</Course>
<Course>Computer Organization and Systems</Course>
<Course>Introduction to Probability for Computer Scientists</Course>
<Course>Digital Systems II</Course>
</CoursesWithPrerequsiteOfEric>
编辑
很抱歉没有首先了解原始要求。您的中间步骤当然是找到所有以 'CS106A' 或 'CS106B' 作为先决条件的课程。但是显然上面的key lookup解决了实际需求
<xsl:template match="/">
<CoursesWithPrerequsiteOfEric>
<xsl:apply-templates select="//Course[Prerequisites/Prereq = 'CS106A' or
Prerequisites/Prereq = 'CS106B']"/>
</CoursesWithPrerequsiteOfEric>
</xsl:template>
回复:更多要求
好吧,您可以内联密钥,但它变得更难理解,IMO:
select="//Course[Prerequisites/Prereq = //Course[Instructors/Professor/First_Name='Eric']/@Number]"
然后重新计算 - 您需要复习一下 xsl functions。
<xsl:value-of select="count(//Course[Prerequisites/Prereq = //Course[Instructors/Professor/First_Name='Eric']/@Number])"/>