擅长:python、mysql、java
<p>如果您喜欢更具声明性的编写规则的方式(而不是python&lxml的过程化方法),也可以使用xslt2.0来实现这一点。在</p>
<p>比如:</p>
<pre><code><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="text" />
<xsl:template match="SerialNumber[matches( normalize-space(.), '^\d{6}$')]" >
<xsl:value-of select="." /> Passes.
</xsl:template>
<xsl:template match="SerialNumber[not( matches( normalize-space(.), '^\d{6}$'))]" >
<xsl:value-of select="." /> Fails.
</xsl:template>
<xsl:template match="text()">
<! override default template, output nothing >
</xsl:template>
</xsl:stylesheet>
</code></pre>
<p>将输出:</p>
<p>154236次传球。在</p>
<p>X154236失败。在</p>
<p>如果有很多规则需要检查,也许应该看看XML模式语言
比如<a href="http://relaxng.org/" rel="nofollow">Relax NG</a>或{a2}。Schema是为比dtd更具表现力的XML文档编写语法的一种方法。使用模式语言编写声明性规则和,处理器编写XSLT代码,根据模式验证XML。在</p>