dongle2627 2017-09-10 13:40
浏览 137
已采纳

如何使用HTML5 data- *属性验证文档?

I need to validate XML with dynamic attribute names, like data-*. Now I'am using RelaxNG schema, but it does not supports dynamic attribute names. What are the options? I cannot find anything 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>
  • 写回答

1条回答 默认 最新

  • dongye9228 2017-09-10 23:26
    关注

    Preprocess the XML to drop the data-* attributes before giving it to the validation function. There is otherwise no way I know to validate it with RelaxNG or other grammar-based schema languages.

    As far as preprocessing the XML, one way to do that with an existing XML toolchain would be: run it through an XSLT transformation that drops the data-* attributes but passes on all 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-')]"/> is the important part there. That causes any data-* attribute to just be dropped on the floor. The rest of that XSL stylesheet is just a basic “identify transform” that passes on everything else from the source XML as-is.

    The W3C Nu Html Checker (HTML5 validator) backend does something for data-* attributes that’s functionally the same as that XSLT transformation, but written in Java. If you’re curious, the code for it is within the GitHub repo for the 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 essentially a SAX filter that works at parse time off parse events prior to the validation function.

    And if you’re even more curious, there is code for other preprocessing filters doing similar things:

    Anyway, you get the general idea: If there are any cases of markup constructs in your source that you can’t express validation logic for in RelaxNG or XSD, then you essentially filter (preprocess) the source to hide that markup from the validation function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端