Identify xml text elements with Schematron

吃可爱长大的小学妹 提交于 2020-01-03 03:24:32

问题


Is it possible to make a rule in XPath which identifies xml elements like this one:

<A>something...<A>

?

I am using Schematron and I need to specify that some elements must not have children like the one in the example, that's why I need to identify them.

Thanks in advance


回答1:


For the following input:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <a>Only Text</a>
  <a><b>Child node</b></a>  
  <a><b>Child node</b>Mixed content</a>
</root>

These Schematron rules should do what you want:

<?xml version="1.0" encoding="UTF-8"?>
<iso:schema xmlns:iso="http://purl.oclc.org/dsdl/schematron">
<iso:pattern id="children tests">
    <iso:rule context="a">
        <iso:assert test="child::node()">
            Element has nodes
        </iso:assert>
        <iso:report test="child::*">
            Element has child elements
        </iso:report>
        <iso:assert test="empty(child::text())">
            Element has text
        </iso:assert>
        <iso:report test="child::text() and empty(child::*)">
            Element has only text
        </iso:report>               
    </iso:rule>
</iso:pattern>
</iso:schema>



来源:https://stackoverflow.com/questions/14651586/identify-xml-text-elements-with-schematron

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!