问题
Part of my project involves heavy transformations of user-defined schema documents. I need to be able to change target namespaces, support type cross-references and build wsdl
upon multiple schema documents.
One of the most commonly used operations is changing namespace prefix prior to importing schema into wsdl file. I'm using org.xml.sax.ContentHandler
and it's startPrefixMapping
method to handle namespaces. All works fine and flawless unless I want to change element types.
Here is simple schema fragment
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<complexType name="Param">
<sequence>
<element name="key" type="string"/>
<element name="value" type="string"/>
</sequence>
</complexType>
<!-- omitted -->
which must be placed inside wsdl as follows:
<definitions xmlns:xs="http://www.w3.org/2001/XMLSchema">
<types>
<xs:schema>
<xs:complexType name="Param">
<xs:sequence>
<xs:element name="key" type="xs:string"/> <!-- 'xs:' to be added -->
<xs:element name="value" type="xs:string"/> <!-- 'xs:' to be added -->
</xs:sequence>
</xs:complexType>
<!-- omitted -->
The problem is that some schema's attribute values (type
in <element>
, base
in <extension>
) are namespace-aware and are subject to change in the above example. As I can see, neither DOM, nor SAX parsers are able to handle this situation so I'm currently using ugly string operations to retrieve namespace info from particular attribute values.
Am I missing somethiing? Are there any api's, libraries or other schema-specific tools to handle such sort of tasks?
回答1:
yes, this is a major bug in xml schema/wsdl. utilizing the xml namespace prefixes inside of attribute values was a giant mistake (since the prefix values themselves are not significant, only placeholders for the actual namespaces). unfortunately, i don't know of any good solution to the problem (i've had to implement virtually the same sort of idea: merging multiple user defined schemas into one schema/wsdl). i do know that xerces has an xml schema model api, but i don't know if it has support for merging together multiple schemas and generating the resulting output.
回答2:
SAX is a very low level to be using for reading schema documents. You'll remove a lot of hassle if you move to a higher level interface like XSLT. You'll remove even more hassle if you read the schema documents using a schema processor that turns them into a schema component model - both Xerces and Saxon can do this, and they aren't alone.
来源:https://stackoverflow.com/questions/7877684/xml-schema-processing-namespace-aware-attribute-values