纷繁的 XML Schema 技术:基于规则的语言[4]
[入库:2005年8月18日] [更新:2007年3月25日]
<xsl:apply-templates select="*|@*|text()"/>
</xsl:template>
<xsl:template match="library/text()[normalize-space()]">
<xsl:message terminate="no">
<xsl:text>the "library" element should have no text, "</xsl:text>
<xsl:value-of select="normalize-space()"/>
<xsl:text>" shouldn't appear!</xsl:text>
</xsl:message>
<xsl:apply-templates select="*|@*|text()"/>
</xsl:template>
<xsl:template match="book/@*">
<xsl:message terminate="no">
<xsl:text>the "book" element should have no other attribute than "id", </xsl:text>
<xsl:value-of select="name()"/>
<xsl:text> shouldn't appear!</xsl:text>
</xsl:message>
<xsl:apply-templates select="*|@*|text()"/>
</xsl:template>
<xsl:template match="book/@id">
<xsl:if test=". = ../preceding-sibling::book/@id">
<xsl:message terminate="no">
<xsl:text>the "book" id should be unique, </xsl:text>
<xsl:value-of select="."/>
<xsl:text> is duplicated.</xsl:text>
</xsl:message>
</xsl:if>
<xsl:apply-templates select="*|@*|text()"/>
</xsl:template>
</xsl:stylesheet>
2.1.2. 封闭的 xslt 实现的 schema
a closed schema is the other way round and will define defaults templates which are forbidding everything (except eventually "empty" text nodes):
封闭的 schema 是反其道而行之,定义一个禁止所有东西的缺省模板(除了最终的 "空" 文本):
<xsl:template match="*">
<xsl:message terminate="no">
<xsl:text>forbidden element:</xsl:text>
<xsl:value-of select="name()"/>
</xsl:message>
<xsl:apply-templates select="*|@*|text()"/>
</xsl:template>
<xsl:template match="@*">
<xsl:message terminate="no">
<xsl:text>forbidden attribute:</xsl:text>
<xsl:value-of select="name()"/>
</xsl:message>
</xsl:template>
<xsl:template match="text()"/>
<xsl:template match="text()[normalize-space()]">
<xsl:message terminate="no">
<xsl:text>forbidden text:</xsl:text>
<xsl:value-of select="."/>
</xsl:message>
</xsl:template>
and then define everything which is allowed, ie in fact very few things:
然后定义出所有允许的东西,事实上也就是一点东西:
<xsl:template match="/library">
<xsl:apply-templates select="*|@*|text()"/>
</xsl:template>
<xsl:template match="library/book">
<xsl:apply-templates select="*|@*|text()"/>
</xsl:template>
<xsl:template match="book/@id[not (.=../preceding-sibling::book/@id)]">
<xsl:apply-templates select="*|@*|text()"/>
本文关键:XML Schematron Rule XSLT
本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)