XPATH Select child elements whose type's element name is xs:simpleType
I am trying to select all child elements in the sequence whose implemented type's element name is of xs:simpleType.
<xs:complexType name="Truck">
<xs:complexContent>
<xs:extension base="Car">
<xs:sequence>
<xs:element name="DriverName" type="Name">
...
</xs:element>
<xs:element name="Engine" type="TruckEngine">
...
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Where the type "Name" looks like:
<xs:simpleType name="Name">
...
</xs:simpleType>
and "TruckEngine" looks like:
<xs:complexType name="TruckEngine">
...
</xs:complexType>
How would I create an XPATH to select the element DriverName? So far I have something like xs:complexType/xs:complexContent/xs:extension/xs:sequence/xs:element, but I don't know how improve my selection any further. I have had a really difficult time searching for relevant information as I am not sure what terminology to use to select something like this. I have modified my example to be something more generic.
You could use:
//xs:element[@type = //xs:simpleType/@name]
although I'd recommend a more explicit path instead of using //
.
(Edit: Changed eq
to =
)
Trying to extract information from a schema document is difficult (almost impossible) unless you know the conventions used by the schema author. For example, the solution from DevNull (which you accepted) will only work if the type is defined in the same schema document, it will only work for a named (not anonymous) type, and it will only work for schema documents with no targetNamespace.
For a more general solution, that works with any schema regardless of authoring convention, don't try to do it this way: use a schema API that gives you access to the "cooked" schema components following analysis by a schema processor. One approach is to use Xerces which offers a Java API to schema components, another is to use Saxon's SCM format which provides an XML representation of the "cooked" schema components in a single document.
链接地址: http://www.djcxy.com/p/34332.html上一篇: 使用XSLT根据XSD转换XML