基于位于几个其他XML文件中的对象的值,使用XSD模式验证标记的值。

huangapple go评论48阅读模式
英文:

Validate the value of a tag based on the values of objects located in several other xml files using XSD schema

问题

你想要验证 XML 中的一个标签的值是否与位于不同文件中的其他对象的标签值相符,使用 XML Schema。您有多个类似于以下内容的 Scripts.xml 文件:

Script1.json

<script>
  <scriptname>script1</scriptname>
  <type>script</type>
</script>

Script2.json

<script>
  <scriptname>script2</scriptname>
  <type>script</type>
</script>

这些文件具有以下模式:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="script">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="scriptname" type="xs:string"/> 
                <xs:element name="type" type="xs:string"/> 
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

然后,您有另一个名为 UseScript.xml 的文件:

<use_script>
  <script>script1</script>
</use_script>

具有以下模式:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="use_script">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="script" type="xs:string"/> 
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

您想要的是验证器搜索文件夹中所有类型为 "script" 的 XML 文件,然后将其与 UseScript.xml 中字段 "script" 的值进行比较。

您想要知道如何在模式中引用 script1.xml 中的 scriptname 吗?是否可能?

可以使用 keyref 元素在 XML Schema 中实现这一目标,但要注意,XML Schema 1.0 不支持在不同文件之间的引用。如果您希望在不同文件中引用,请考虑将所有相关内容合并到一个文件中。

英文:

I want to validate that the value of a tag in xml coincides with the values of the tags of other objects located in different files using XML Schema.

Take that I have several Scripts.xml files like this:

Script1.json

&lt;script&gt;
  &lt;scriptname&gt;script1&lt;/scriptname&gt;
  &lt;type&gt;script&lt;/type&gt;
&lt;/script&gt;

Script2.json

&lt;script&gt;
  &lt;scriptname&gt;script2&lt;/scriptname&gt;
  &lt;type&gt;script&lt;/type&gt;
&lt;/script&gt;

These files have a schema like this

<?xml version="1.0"?>

&lt;xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
    &lt;xs:element name=&quot;script&quot;&gt;
        &lt;xs:complexType&gt;
            &lt;xs:sequence&gt;
                &lt;xs:element name=&quot;scriptname&quot; type=&quot;xs:string&quot;/&gt; 
                &lt;xs:element name=&quot;type&quot; type=&quot;xs:string&quot;/&gt; 
            &lt;/xs:sequence&gt;
        &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
&lt;/xs:schema&gt;

Then I have another file called UseScript.xml

&lt;use_script&gt;
  &lt;script&gt;script1&lt;/script&gt;
&lt;/use_script&gt;

with schema

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
    &lt;xs:element name=&quot;use_script&quot;&gt;
        &lt;xs:complexType&gt;
            &lt;xs:sequence&gt;
                &lt;xs:element name=&quot;script&quot; type=&quot;xs:string&quot;/&gt; 
            &lt;/xs:sequence&gt;
        &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
&lt;/xs:schema&gt;

What I look is that the validator search all the xml files in a folder with type "script" and then compare it with the value of field "script" in UseScript.xml.

How can I reference the scriptname from the script1.xml in the schema?
Is it possible at all?

答案1

得分: 1

XSD 无法跨多个文件进行验证。即使使用 XSD 1.1 断言,也无法实现,尽管您可能可以使用供应商扩展来实现它。

一个解决方法是在验证之前将所有的 XML 组合到单个文件中。您可以使用 XSLT 转换来实现这一点,如果您有一个具备模式感知的 XSLT 处理器,实际上可以在 XSLT 代码内部验证转换结果,使其成为一个单步操作。

另一个解决方案是使用不同的技术进行验证。我不确定 Schematron 或 RelaxNG 是否可以处理它(有人可能会纠正我)。但不要忽视直接在 XSLT 或 XQuery 中进行验证的可能性。

英文:

XSD can't do validation across multiple files. That's true even with XSD 1.1 assertions, though you might be able to do it using vendor extensions.

One workaround is to combine all your XML into a single file prior to validation. You could do this using an XSLT transformation, and if you have a schema-aware XSLT processor, you could actually validate the transformation result from within the XSLT code itself so it becomes a single-step operation.

The other solution is to use a different technology for the validation. I'm not sure Schematron or RelaxNG can handle it either (someone might correct me). But don't overlook the possibility of doing the validation directly in XSLT or XQuery.

huangapple
  • 本文由 发表于 2023年5月11日 15:00:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定