如何使用具有命名空间和命名空间前缀的 typescript/javascript 验证 xml 和 xsd?

How to validate xml and xsd using typescript/javascript that has namespace and namespace prefixes?

提问人:Raviraj Bhalerao 提问时间:8/31/2023 更新时间:8/31/2023 访问量:37

问:

我有以下 xsd -

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
        targetNamespace="http://sg.public.org/test.xsd"
        xmlns:sg="http://sg.public.org/test.xsd"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="EmployeeDetails">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="FirstName" type="xs:string"/>
                <xs:element name="MiddleName">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:minLength value="1"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="LastName" type="xs:string" />
                <xs:element name="EmailId" type="xs:string" />
                <xs:element name="Mobile" type="xs:integer" minOccurs="1" maxOccurs="1"/>
                <xs:element name="Years">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Year" type="sg:Year" minOccurs="1" maxOccurs="unbounded"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Address">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:minLength value="5"/>
                            <xs:maxLength value="100"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="Cars" type="sg:Cars"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="Cars">
        <xs:sequence>
            <xs:element name="Car" type="sg:Car" maxOccurs="unbounded">
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Car">
        <xs:simpleContent>
            <xs:extension base="sg:CarModel">
                <xs:attribute name="id" type="xs:string" use="required"/>
                <xs:attribute name="year" type="sg:Year" use="required"/>
                <xs:attribute name="condition" type="xs:string" use="optional"  />
                <!--fixed="count(./ancestor::sg:EmployeeDetails/Year[text() &lt;= [@year]])=1"/>-->
                <xs:attribute name="message" type="xs:string" use="optional" fixed="Driver must be 100 yrs old."/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:simpleType name="CarModel">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Audi"/>
            <xs:enumeration value="Golf"/>
            <xs:enumeration value="BMW"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="Year">
        <xs:restriction base="xs:int">
            <xs:minInclusive value="1900" />
            <xs:maxInclusive value="2200" />
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

和 xml 如下所示 -

<?xml version="1.0" encoding="utf-8"?>
<sg:EmployeeDetails xmlns:sg="http://sg.public.org/test.xsd">
    <sg:FirstName>Sagar</sg:FirstName>
    <sg:MiddleName>H</sg:MiddleName>
    <sg:LastName>Shinde</sg:LastName>
    <sg:EmailId>[email protected]</sg:EmailId>
    <sg:Mobile>9021063389</sg:Mobile>
    <sg:Years>
        <sg:Year>1954</sg:Year>
        <sg:Year>1980</sg:Year>
        <sg:Year>1954</sg:Year>
    </sg:Years>
    <sg:Address>135/214, Hindustan Chowk, Mulund Colony, Mulund (W) - 400082</sg:Address>
    <sg:Cars>
        <sg:Car id="car2" year="1954" condition="let $car := . return count(ancestor::sg:EmployeeDetails/sg:Years/sg:Year[text() = $car[@year]]) &gt;0" message="Driver must be 100 yrs old.">BMW</sg:Car>
        <sg:Car id="car1" year="1980"  condition="count(./ancestor::sg:EmployeeDetails/sg:Years/sg:Year[text() = //sg:Car[@id='car1']/@year]) &gt;=0" message="Driver must be 100 yrs old.">BMW</sg:Car>
    </sg:Cars>
</sg:EmployeeDetails>

我想使用 typescript/javascript 使用 xsd 验证此 xml。

我知道“xsd-schema-validator”包。我不确定如何使用命名空间。

如果你知道别的东西,那也没关系。

但是,我必须使用命名空间及其前缀对其进行验证。

javascript typescript xml-namespaces xsd-validation

评论


答: 暂无答案