php - How to validate documents with HTML5 data-* attributes? -


i need validate xml dynamic attribute names, data-*. i'am using relaxng schema, not supports dynamic attribute names. options? cannot find relevant..

example of xml:

<?xml version="1.0" encoding="utf-8"?> <body xml:lang="cs" ns="www.x.y">   <h id="x" ctime="2017-09">heading..</h>   <desc kw="kw">desc..</desc>   <section>     <h data-foo="bar" id="one" short="one">first heading</h>     <desc>desc...</desc>     <p>content..</p>     <ul data-buz="fuz">       <li data-switch="click">list item</li>       <li>list item 2</li>     </ul>   </section> </body> 

preprocess xml drop data-* attributes before giving validation function. there otherwise no way know validate relaxng or other grammar-based schema languages.

as far preprocessing xml, 1 way existing xml toolchain be: run through xslt transformation drops data-* attributes passes on else as-is:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version='1.0'>   <xsl:output method="xml" indent="no"/>   <xsl:template match="node() | @*">     <xsl:copy>       <xsl:apply-templates select="@* | node()"/>     </xsl:copy>   </xsl:template>   <xsl:template match="@*[starts-with(name(), 'data-')]"/> </xsl:stylesheet> 

the <xsl:template match="@*[starts-with(name(), 'data-')]"/> important part there. causes data-* attribute dropped on floor. rest of xsl stylesheet basic “identify transform” passes on else source xml as-is.

the w3c nu html checker (html5 validator) backend data-* attributes that’s functionally same xslt transformation, written in java. if you’re curious, code within the github repo w3c nu html checker sources, here:

https://github.com/validator/validator/tree/master/src/nu/validator/xml/dataattributes

see the filterattributes code in dataattributedroppingcontenthandlerwrapper.java

it’s sax filter works @ parse time off parse events prior validation function.

and if you’re more curious, there code other preprocessing filters doing similar things:

anyway, general idea: if there cases of markup constructs in source can’t express validation logic in relaxng or xsd, filter (preprocess) source hide markup validation function.


Comments