正在回答 回答被采纳积分+1
同学你好!
这是由于同学xsd文件没有修改呢,老师对同学的xml和xsd都做了修改,同学可以参考一下:
xml:
<?xml version="1.0" encoding="UTF-8"?> <teaching-plan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="plan.xsd"> <course id="001"> <course-name>大学英语</course-name> <class-hour>36</class-hour> <exam-form>考试</exam-form> </course> <course id="002"> <course-name>高等数学1</course-name> <class-hour>70</class-hour> <exam-form>考试</exam-form> </course> <course id="003"> <course-name>计算机应用基础</course-name> <class-hour>108</class-hour> <exam-form>上机考试</exam-form> </course> <course id="004"> <course-name>Java编程</course-name> <class-hour>100</class-hour> <exam-form>上机考试</exam-form> </course> <course id="005"> <course-name>艺术插花</course-name> <class-hour>25</class-hour> <exam-form>开卷考试</exam-form> </course> </teaching-plan>
xsd:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" > <xs:element name="teaching-plan"> <xs:complexType> <xs:sequence> <xs:element name="course" maxOccurs="100"> <xs:complexType> <xs:sequence> <xs:element name = "course-name" type="xs:string"></xs:element> <xs:element name = "class-hour"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="20"></xs:minInclusive> <xs:maxInclusive value="110"></xs:maxInclusive> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name = "exam-form" type = "xs:string"></xs:element> </xs:sequence> <xs:attribute name="id" type="xs:string" use="required"></xs:attribute> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">
<xs:element name="teaching-plan">
<xs:complexType>
<xs:sequence>
<xs:element name="course" maxOccurs="100">
<xs:complexType>
<xs:sequence>
<xs:element name = "course-name" type="xs:string"></xs:element>
<xs:element name = "class-hour">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="20"></xs:minInclusive>
<xs:maxInclusive value="110"></xs:maxInclusive>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name = "exam-form" type = "xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
/* 以下为Java代码*/
package xml;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.util.List;
public class TeachPlanXpath {
// private String file = "D:\\java\\01_developer\\java_module3\\javaclass03\\src\\xml\\plan.xml";
public void Xpath(String xpathExp){
String file = "D:\\java\\01_developer\\java_module3\\javaclass03\\src\\xml\\plan.xml";
SAXReader reader = new SAXReader();
try {
Document document = reader.read(file);
// System.out.println(document);
List<Node> nodes = document.selectNodes(xpathExp);
// System.out.println(nodes);
for(Node node : nodes) {
Element course = (Element) node;
System.out.println("课程ID为:"+course.attributeValue("id"));
System.out.println("课程名称为:"+course.elementText("course-name"));
System.out.println("课时为:"+course.elementText("class-hour"));
System.out.println("考试形式为:"+course.elementText("exam-form"));
System.out.println("----------------------------------------------------");
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TeachPlanXpath teachPlan = new TeachPlanXpath();
//teachPlan.Xpath("//course");
// teachPlan.Xpath("//course[class-hour<50]");
//teachPlan.Xpath("//course[course-name='高等数学']");
//teachPlan.Xpath("//course[@id=001]");
teachPlan.Xpath("//course[position()<3]");
}
}
<!-- 以下为plan.xml文件-->
<?xml version="1.0" encoding="UTF-8"?>
<teaching-plan xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="plan.xsd">
<course id="001">
<course-name>大学英语</course-name>
<class-hour>36</class-hour>
<exam-form>考试</exam-form>
</course>
<course id="002">
<course-name>高等数学</course-name>
<class-hour>70</class-hour>
<exam-form>考试</exam-form>
</course>
<course id="003">
<course-name>计算机应用基础</course-name>
<class-hour>108</class-hour>
<exam-form>上机考试</exam-form>
</course>
<course id="004">
<course-name>Java编程</course-name>
<class-hour>100</class-hour>
<exam-form>上机考试</exam-form>
</course>
<course id="005">
<course-name>艺术插花</course-name>
<class-hour>25</class-hour>
<exam-form>开卷考试</exam-form>
</course>
</teaching-plan>
同学的问题和这个同学的有些类似,可以参考一下这个同学的回答:https://class.imooc.com/course/qadetail/97176
但看起来同学的这个用的也是不带命名空间的
如果还是没有解决,请同学贴一下你的代码,老师尝试调试一下。
注意不要贴在回复里,会失去代码的格式。可以在回答里贴一下。祝学习愉快。
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星