XML、XSD 和 XSLT 学生项目



我必须为我的大学项目制作xml,xml模式和xslt。我从验证器收到 3 个错误。你可以帮我吗? 那是我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="transform.xslt"?> 
<mojehobby>
<naglowek>
<naglowek_glowny>
Moje hobby
</naglowek_glowny>
<podnaglowek>
muzyka 
</podnaglowek>
</naglowek>
<images>
<zdjecie src="AP4.jpg" href="https://www.facebook.com/AfterPeppers/"></zdjecie>
<zdjecie src="COSTER1.jpg" href="https://www.facebook.com/Coster-433619970040235/"></zdjecie>
<zdjecie src="TSF2.jpg" href="https://www.facebook.com/The-Second-Floor-200535016957502/"></zdjecie>                
</images>
<dana>
<artykul>
<tytul>
Historia
</tytul>
<naglowek_artykulu>
O muzyce i nauce
</naglowek_artykulu>
<tresc_artykulu>     
<tresc>
Some text
</tresc>
</tresc_artykulu>
</artykul>
</dana>
<o_artykule>
<autor>
<imie_autora>Krzysztof</imie_autora>
<nazwisko_autora>Stencel</nazwisko_autora>
</autor>
<data>15.12.2019</data>
</o_artykule>
<stopka href="">
<tresc></tresc>
<data></data>
</stopka>
</mojehobby>

那是 XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="krotki_string">
<xs:restriction base="xs:string">
<xs:maxLength value="30"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="article">
<xs:restriction base="xs:string">
<xs:minLength value="400"/>
</xs:restriction>
</xs:simpleType>    
<xs:simpleType name="imie">
<xs:restriction base="xs:string">
<xs:maxLength value="25"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="nazwisko">
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="data">
<xs:restriction base="xs:string">
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="tresc">
</xs:complexType>
<xs:complexType name="image">
<xs:attribute name="src"/>
<xs:attribute ref="href"/>
</xs:complexType>   
<xs:complexType name="images">
<xs:sequence>
<xs:element name="zdjecie" type="image" maxOccurs="3"/>
</xs:sequence>
</xs:complexType>   
<xs:complexType name="tresc_artykulu">
<xs:sequence>
<xs:element name="tresc" type="article" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="footer">
<xs:sequence>
<xs:element name="tresc" type="krotki_string"/>
<xs:element name="data" type="data"/>
</xs:sequence>  
</xs:complexType>

<xs:element name="o_artykule">
<xs:complexType>
<xs:sequence>
<xs:element name="autor" type="simpleType">
<xs:complexType>
<xs:sequence>
<xs:element name="imie_autora" type="imie"/>
<xs:element name="nazwisko_autora" type="nazwisko"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="data" type="data"/>
</xs:sequence>  
</xs:complexType>   
</xs:element>
<xs:element name="naglowek">
<xs:complexType>
<xs:sequence>
<xs:element name="naglowek_glowny" type="krotki_string"/>
<xs:element name="podnaglowek" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>   
<xs:element name="dana">
<xs:sequence>
<xs:complexType name="artykul">
<xs:sequence>
<xs:element name="tytul" type="krotki_string"/>
<xs:element name="naglowek_artykulu" type="krotki_string"/>             
</xs:sequence>
</xs:complexType>
<xs:complexType name="tresc_artykulu">
<xs:sequence>
<xs:element name="tresc" type="article" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>   
</xs:sequence>
</xs:element>
<xs:element name="mojehobby">
<xs:complexType>
<xs:sequence>
<xs:element ref="naglowek"/>
<xs:element name="images" type="images"/>
<xs:element ref="dana"/>
</xs:sequence>
</xs:complexType>   
</xs:element>
<xs:attribute name="href"/>
</xs:schema>    

并且有错误:

Validation  87, 15  s4s-elt-must-match.1: The content of 'dana' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: sequence.
Validation     64, 47  src-element.3: Element 'autor' has both a 'type' attribute and a 'anonymous type' child. Only one of these is allowed for an element.
Validation     40, 15  cvc-complex-type.2.4.d: Invalid content was found starting with element 'o_artykule'. No child element is expected at this point.

关于错误:

  1. 错误

    验证 87, 15 s4s-elt-must-match.1: 'dana' 的内容必须匹配 (annotation?, (simpleType | complexType(?, (unique | key | keyref(*((。从 开始发现一个问题:序列。

    发出,因为您忘记将 xs:序列包装在 xs:complexType 中。您在以下 xs:elementmojehobby中做对了。

  2. 接下来,同样在dana中,您尝试在序列中定义 xs:complexType - 这是不允许的。合并 xs:sequence 中的所有元素将修复它。

  3. 错误

    验证 64, 47 src-element.3:元素 'autor' 同时具有 'type' 属性和 'anonymous type' 子属性。元素只允许其中之一。

    仅指您尝试将两种类型应用于同一元素的事实。摆脱一个,你会没事的。(此处:type属性(

  4. 错误

    验证 40, 15 cvc-complex-type.2.4.d:发现以元素"o_artykule"开头的无效内容。此时不应有子元素。

    确实会发生,因为您忘记将 XS:元素作为mojehobby规则的子级包含在内。这也适用于stopka及其子项。

我还在 XSD 文件中添加了一些提示。它仍然会抛出错误,因为内容无效:多个节点的内容超出了字符串类型中定义的 xs:maxLength 限制。
但是,这是经过调试的工作版本:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="krotki_string">
<xs:restriction base="xs:string">
<xs:maxLength value="30"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="article">
<xs:restriction base="xs:string">
<xs:minLength value="400"/>
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="imie">
<xs:restriction base="xs:string">
<xs:maxLength value="25"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="nazwisko">
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="data">
<xs:restriction base="xs:string">
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="tresc">
</xs:complexType>
<xs:complexType name="image">
<xs:attribute name="src"/>
<xs:attribute ref="href"/>
</xs:complexType>

<xs:complexType name="images">
<xs:sequence>
<xs:element name="zdjecie" type="image" maxOccurs="3"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="tresc_artykulu">
<xs:sequence>
<xs:element name="tresc" type="article" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="footer">
<xs:sequence>
<xs:element name="tresc" type="krotki_string"/>
<xs:element name="data" type="data"/>
</xs:sequence>
<xs:attribute ref="href"/>
<!-- Note that attribute definitions have to appear at the end of the complex type - here referencing the attribute at the end of the file -->
</xs:complexType>
<xs:element name="o_artykule">
<xs:complexType>
<xs:sequence>
<xs:element name="autor">
<xs:complexType>
<xs:sequence>
<xs:element name="imie_autora" type="imie"/>
<xs:element name="nazwisko_autora" type="nazwisko"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="data" type="data"/>
</xs:sequence>            
</xs:complexType>        
</xs:element>
<xs:element name="naglowek">
<xs:complexType>
<xs:sequence>
<xs:element name="naglowek_glowny" type="krotki_string"/>
<xs:element name="podnaglowek" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="dana">
<xs:complexType>
<xs:sequence>
<xs:element name="artykul">
<xs:complexType>
<xs:sequence>
<xs:element name="tytul" type="krotki_string"/>
<xs:element name="naglowek_artykulu" type="krotki_string"/>
<xs:element name="tresc_artykulu" type="tresc_artykulu" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>        
</xs:element>
<xs:element name="mojehobby">
<xs:complexType>
<xs:sequence>
<xs:element ref="naglowek"/>
<xs:element name="images" type="images"/>
<xs:element ref="dana"/>
<xs:element ref="o_artykule"/>            <!-- Added element for reference type -->
<xs:element name="stopka" type="footer"/> <!-- Added element for defined type -->
</xs:sequence>
</xs:complexType>        
</xs:element>
<xs:attribute name="href"/>
</xs:schema>

希望这有帮助。

最新更新