开发者

XML Schema type definition for XPath values?

I have an XML file where I have an attribute whose value will be an XPath to locate content in a different set of XML files.

Example:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <IterestingNode Value="/html/head/title"/>
</Root>

This file can be validated against a XSD. Currently I just validate that the attribute's value is a string, but I'd like to check that it is a sintactically valid XPath value.

I开发者_如何学运维s there a XML Schema definition for XPath values?


There is not an XSD data type for an XPATH expression.

The best that you could probably do would be to use an xsd:restriction with an xsd:pattern that uses a regex pattern to validate that the string value is an XPATH expression.

Something like this:

<xsd:simpleType name="XPathValueType">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="**PUT IN SOME REGEX PATTERN TO VERIFY THE STRING IS AN XPATH EXPRESSION**"/>
  </xsd:restriction>
</xsd:simpleType>

I'm not aware of an regex pattern that will validate that a given string is an XPATH expression to reference.


I doubt it xpath is data, not XML. The spec is here : http://www.w3.org/TR/xpath/


You could use anyURI with some pattern, and XPointer

XPointer example:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <IterestingNode Value="doc.xml#xpointer(/html/head/title)"/>
</Root>


Ultimately, the answer provided by redben is the correct answer (indeed I have seen it claimed, and I believe that xslt cannot be expressed in xsd), but I should like to draw attention to the xslt xsd which has the following xsd definition for an xpath expression:

<xs:simpleType name="expression">
    <xs:annotation>
        <xs:documentation>
            An XPath 2.0 expression.
        </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:token">
        <xs:pattern value=".+"/>
    </xs:restriction>
</xs:simpleType>

Whilst I realise this question has not been active for a long while, I thought this might be instructive to anyone else who should happen upon this subject.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜