Search code examples
xmlxpathdtd

Can we use Xpath on DtD


I'd like to find specific XML_ELEMENT_DECL type nodes, without linearly going through the tree, is there a way to use Xpath to do that?

Using the XML example, how do I find the XML_ELEMENT_DECL version of "UID" instead of the XML_ELEMENT_NODE version?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE TSS [
    <!ELEMENT TSS (name, UID, Sequences*)>
    <!ATTLIST TSS doc CDATA #IMPLIED>
    <!ATTLIST TSS help CDATA "">
    <!ATTLIST TSS image CDATA #FIXED "C:\Users\dholstein\Documents\XML\icons\settings.png">
      <!ELEMENT name (#PCDATA)>
      <!ELEMENT UID (#PCDATA)>
      <!ELEMENT Sequences (Sequence*)>
        <!ELEMENT Sequence (#PCDATA|Steps)*>
            <!ELEMENT Steps (Step*, dan1, dan2, dan3)>
            <!ELEMENT Step (#PCDATA)>
]>
<TSS doc="da man" image="C:\Users\dholstein\Documents\XML\icons\settings.png">
    <name><![CDATA[X-32 T&I]]></name>
    <UID> <![CDATA[123456]]></UID>
    <Sequences>
        <Sequence><![CDATA[DMWR Test]]>
            <Steps>
                <Step><![CDATA[Step 1]]>
                </Step>
                <Step><![CDATA[Step 2]]>
                </Step>
            </Steps>
        </Sequence>
        <Sequence><![CDATA[Verification]]></Sequence>
    </Sequences>
</TSS>
<!-- /TSS/Sequences/Sequence[contains(text(), 'DMWR')]/Steps/Step[contains(text(), 'Step 1')] -->

Solution

  • No, XPath cannot be used to select parts of a DTD.

    XPath operates over XML, and DTDs are not represented in XML.

    XSDs, on the other hand, are XML and may therefore be used with XPaths.

    Use XSD rather than DTD for any new XML schema development work; they supercede DTDs in many useful ways, including being represented themselves as XML.