如何根據給定的xsd-schema將無效的XML轉換爲有效的XML? 例如,我有下一個XSD架構:如何將無效的XML轉換爲有效的XML
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
和明年無效XML:
<?xml version="1.0" encoding="UTF-8"?>
<note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../my_xsd.xsd">
<to>[email protected]</to>
<from>[email protected]</from>
<body>blablabla</body> <!-- IVALID LINE, IT IS NOT IN RIGHT PALCE -->
<heading>head</heading>
</note>
我的問題是:做JAXB,XSTREAM,或其它XML解析器有解決方案轉換,根據我的無效的XML定模式,以有效的XML:
<?xml version="1.0" encoding="UTF-8"?>
<note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../my_xsd.xsd">
<to>[email protected]</to>
<from>[email protected]</from>
<heading>head</heading>
<body>blablabla</body>
</note>
實際 - 是的。我期望得到速度很快的XML,但是元素順序不正確。並且我需要的主要功能 - 按正確順序放置元素和屬性,並根據給定的XSD。 –
@AleksandrZhytnikov:請注意,上述模板旨在與身份模板一起使用(http://en.wikipedia.org/wiki/Identity_transform#Using_XSLT)。或者,您可以用'copy-of'替換'apply-templates'。 – LarsH