- 我為我們的 XML 檔案(XML 提要)應用了一些 XSLT 格式化程式,有點美化,標簽按字母順序排列:
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates>
<xsl:sort select="name()" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates>
<xsl:sort select="name()" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*/*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates>
<xsl:sort select="name()" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*/*/*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates>
<xsl:sort select="name()" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*/*/*/*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates>
<xsl:sort select="name()" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
2.但我們的 XML 檔案中很少有最初使用特定 XSD 模式的應用標簽,該標簽<xs:sequence>要求指定元素和模式驗證器的嚴格順序失敗。
當然,我們可以將這幾個檔案排除在美化之外,但問題是:我們能否以某種方式配置我們的 XSLT 以避免排除這些檔案?XSD 架構無法更改。
uj5u.com熱心網友回復:
首先,您不需要有五個具有不同匹配模式的相同模板規則。一個單一的模板規則match="*"就可以完成這項作業。
其次,無論是否存在模式,元素的順序通常都很重要。有時存在語意意義(需要保留章節中的段落順序,就像文章作者的順序一樣),有時存在字母順序以外的常規順序(表格標題在表格主體之前);有時接收應用程式被寫入期望特定的順序。因此,我建議僅將您的轉換應用于那些元素順序無關緊要的稀有檔案。
uj5u.com熱心網友回復:
您當然可以嘗試/*/*/*/*[not(self::xs:sequence)]在匹配模式中使用謂詞,宣告xmlns:xs="http://www.w3.org/2001/XMLSchema". 我不會嘗試說明哪些??模式需要它,但我認為 XSD 模式不能有xs:sequenceas/*或/*/*.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427387.html
上一篇:使用帶有xml的程序“sp_xml_preparedocument”在SQLServer中決議XML時默認命名空間無法決議
