如果我有一个简单的xsd文件和一个简单的xml文件,那么SaxonJS能显示在xsd中没有定义哪些元素和哪些属性吗?
我一直在寻找例子,但到目前为止还没有找到任何东西。
更新
我还将接受js代码(节点)的回答,它使用saxon遍历xml资源,并检查xsd资源中的元素和属性(不需要检查属性值)。
以一种有效率的方式。
发布于 2021-09-21 20:39:21
简单地检查模式中的xs:element元素是否存在,键就足够了,对于xs:attribute也是如此。但是,所有这些都依赖于所使用的简单xs:element name="foo"和xs:attribute name="att1"声明,而不是检查嵌套或结构:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="element-by-name" match="xs:element" use="QName(/*/@targetNamespace, @name)"/>
<xsl:key name="attribute-by-name" match="xs:attribute" use="QName(/*/@targetNamespace, @name)"/>
<xsl:template match="*[not(key('element-by-name', node-name(), $schema-doc))]">
<element-not-declared>
<name>{node-name()}</name>
<path>{path()}</path>
</element-not-declared>
<xsl:next-match/>
</xsl:template>
<xsl:template match="@*[not(key('attribute-by-name', node-name(), $schema-doc))]">
<attribute-not-declared>
<name>{node-name()}</name>
<path>{path()}</path>
</attribute-not-declared>
</xsl:template>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="/" name="xsl:initial-template">
<xsl:next-match/>
<xsl:comment xmlns:saxon="http://saxon.sf.net/">Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} {system-property('Q{http://saxon.sf.net/}platform')}</xsl:comment>
</xsl:template>
<xsl:param name="schema-doc">
<xs:schema>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:element name="foo" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</xsl:param>
</xsl:stylesheet>样本输入
<?xml version="1.0" encoding="utf-8"?>
<root>
<items count="1">
<item>
<foo>foo 1</foo>
<bar>bar 1</bar>
</item>
</items>
</root>在浏览器中使用Saxon-JS2.3运行时,
<attribute-not-declared>
<name>count</name>
<path>/Q{}root[1]/Q{}items[1]/@count</path>
</attribute-not-declared>
<element-not-declared>
<name>bar</name>
<path>/Q{}root[1]/Q{}items[1]/Q{}item[1]/Q{}bar[1]</path>
</element-not-declared>
<!--Run with Saxon-JS 2.3 Browser-->但我已经测试过,它也适用于“Saxon-JS2.3 Node.js”。
因此,根据模式中使用的键,可以找到一些没有找到匹配声明的元素或属性。它只是一种肤浅的方法,甚至不考虑名称空间、elementForm或attributeForm以及名称空间的复杂性。
要使用SaxonJS运行XSLT 3代码,可以使用SaxonJS.XPath.evaluate调用XPath 3.1 transform函数运行XSLT,如下图所示,也可以首先使用xslt3命令行工具将XSLT导出到SEF/JSON,然后使用SaxonJS.transform运行。
const SaxonJS = require("saxon-js");
const xml = `<?xml version="1.0" encoding="utf-8"?>
<root>
<items count="1">
<item>
<foo>foo 1</foo>
<bar>bar 1</bar>
</item>
</items>
</root>`;
const xsd = `<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="element-by-name" match="xs:element" use="QName(/*/@targetNamespace, @name)"/>
<xsl:key name="attribute-by-name" match="xs:attribute" use="QName(/*/@targetNamespace, @name)"/>
<xsl:template match="*[not(key('element-by-name', node-name(), $schema-doc))]">
<element-not-declared>
<name>{node-name()}</name>
<path>{path()}</path>
</element-not-declared>
<xsl:next-match/>
</xsl:template>
<xsl:template match="@*[not(key('attribute-by-name', node-name(), $schema-doc))]">
<attribute-not-declared>
<name>{node-name()}</name>
<path>{path()}</path>
</attribute-not-declared>
</xsl:template>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="/" name="xsl:initial-template">
<xsl:next-match/>
<xsl:comment xmlns:saxon="http://saxon.sf.net/">Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} {system-property('Q{http://saxon.sf.net/}platform')}</xsl:comment>
</xsl:template>
<xsl:param name="schema-doc">
<xs:schema>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:element name="foo" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</xsl:param>
</xsl:stylesheet>`;
const result = SaxonJS.XPath.evaluate(`
transform(
map {
'source-node' : parse-xml($xml),
'stylesheet-text' : $xsd,
'delivery-format' : 'serialized'
}
)?output`,
[],
{
params : { xml : xml, xsd : xsd }
}
);
console.log(result);输出
<?xml version="1.0" encoding="UTF-8"?>
<attribute-not-declared>
<name>count</name>
<path>/Q{}root[1]/Q{}items[1]/@count</path>
</attribute-not-declared>
<element-not-declared>
<name>bar</name>
<path>/Q{}root[1]/Q{}items[1]/Q{}item[1]/Q{}bar[1]</path>
</element-not-declared>
<!--Run with Saxon-JS 2.3 Node.js-->https://stackoverflow.com/questions/68575216
复制相似问题