开发者

Get duplicate attributes in xml using schematron

I am writing few checks for XML file using schematron.

I want to write down a check so that there must be no duplicate attribute 'id' in any tag across the xml.

Please note, attribute 'id' can occur in any element across the xml.

I found something related to this but开发者_JAVA技巧 that was case for siblings only which used preceding-sibling function.

Please suggest.


This is the approach I use. It's horribly inefficient to execute an XPath statement across the entire document for every element that might have an id attribute. So, I use an xsl:key. The solution below uses ISO Schematron.

<schema xmlns="http://purl.oclc.org/dsdl/schematron"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    queryBinding="xslt2" schemaVersion="ISO19757-3">

    <xsl:key name="xmlid" match="*[@id]" use="@id"/>

    <pattern id="duplicate_id"> 
        <rule context="*[@id]"> 
           <assert test="count(key('xmlid', @id)) = 1">
           Duplicated id in element "<name/>" - "<value-of select='@id'/>".
           </assert>
        </rule> 
    </pattern> 
</schema>

The key caches all the elements that have an id attribute. The rule then applies to all elements with that attribute. The assertion simply counts the number of matches against the id attribute and produces an error message if the count isn't one.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜