如何忽略未知标签的验证?

时间:2021-09-14 15:35:43

One more challenge to the XSD capability,

I have been sending XML files by my clients, which will be having 0 or more undefined or [call] unexpected tags (May appear in hierarchy). Well they are redundant tags for me .. so I have got to ignore their presence, but along with them there are some set of tags which are required to be validated.

对XSD功能的另一个挑战是,我的客户端一直在发送XML文件,这些文件将有0或更多未定义的或[调用]意外标记(可能出现在层次结构中)。它们对我来说是多余的标签。所以我必须忽略它们的存在,但是和它们一起有一些标签是需要验证的。

This is a sample XML:

这是一个示例XML:

<root>
  <undefined_1>one</undefined_1>
  <undefined_2>two</undefined_2>
  <node>to_be_validated</node>
  <undefined_3>two</undefined_3>
  <undefined_4>two</undefined_4>
</root>

And the XSD I tried with:

我尝试过的XSD:

  <xs:element name="root" type="root"></xs:element>
  <xs:complexType name="root">
    <xs:sequence>
      <xs:any maxOccurs="2" minOccurs="0"/>
      <xs:element name="node" type="xs:string"/>
      <xs:any maxOccurs="2" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType

XSD doesn't allow this, due to certain reasons.
The above mentioned example is just a sample. The practical XML comes with the complex hierarchy of XML tags ..

由于某些原因,XSD不允许这样做。上面提到的示例只是一个示例。实用的XML具有复杂的XML标签层次结构。

Kindly let me know if you can get a hack of it.

如果你能弄懂它,请告诉我。

By the way, The alternative solution is to insert XSL-transformation, before validation process. Well, I am avoiding it because I need to change the .Net code which triggers validation process, which is supported at the least by my company.

顺便说一下,替代的解决方案是在验证过程之前插入XSL-transformation。我之所以避免使用它,是因为我需要修改。net代码来触发验证过程,这至少得到了我的公司的支持。

5 个解决方案

#1


3  

In case your not already done with this, you might try the following:

如果你还没有完成这项工作,你可以试试以下方法:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root" type="root"></xs:element>
  <xs:complexType name="root">
    <xs:sequence>
      <xs:any maxOccurs="2" minOccurs="0" processContents="skip"/>
      <xs:element name="node" type="xs:string"/>
      <xs:any maxOccurs="2" minOccurs="0" processContents="skip"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Under Linux this works fine with xmllint using libxml version 20706.

在Linux下,使用libxml版本20706使用xmllint可以正常工作。

#2


2  

Conclusion:

结论:

This is not possible with XSD. All the approaches I was trying to achieve the requirement were named as "ambiguous" by validation-tools, accompanying bunch of errors.

对于XSD,这是不可能的。我试图实现需求的所有方法都被验证工具命名为“歧义”,并伴有大量错误。

#3


1  

Maybe its is possible to use namespaces:

也许使用名称空间是可能的:

<xs:element name="root" type="root"></xs:element> 
  <xs:complexType name="root"> 
    <xs:sequence> 
      <xs:any maxOccurs="2" minOccurs="0" namespace="http://ns1.com" /> 
      <xs:element name="node" type="xs:string"/> 
      <xs:any maxOccurs="2" minOccurs="0" namespace="http://ns2.com"/> 
    </xs:sequence> 
  </xs:complexType>

This will probably validate.

这可能会验证。

#4


1  

I faced the same problem.

我也遇到了同样的问题。

Since I called the validation from .NET; I decided to suppress the specific validation error in ValidationEventHandler as a workaround. It worked for me.

因为我从。net调用了验证;我决定将ValidationEventHandler中的特定验证错误作为解决方案加以抑制。它为我工作。

    private void ValidationEventHandler(object sender, ValidationEventArgs e)
    {
        switch (e.Severity)
        {
            case XmlSeverityType.Warning:
                // Processing warnings
                break;
            case XmlSeverityType.Error:
                if (IgnoreUnknownTags
                    && e.Exception is XmlSchemaValidationException
                    && new Regex(
                        @"The element '.*' has invalid child element '.*'\."
                        + @" List of possible elements expected:'.*'\.")
                       .IsMatch(e.Exception.Message))
                {
                    return;
                }
                // Processing errors
                break;
            default:
                throw new InvalidEnumArgumentException("Severity should be one of the valid values");
        }
    }

It is important that Thread.CurrentUICulture must be set to English or CultureInfo.InvariantCulture for the current thread for this to work.

这条线很重要。当前文化必须设置为英语或文化信息。当前线程的不变性使其工作。

#5


0  

You could make use of a new feature in XML 1.1 called "Open Content". In short, allows you to specify that additional "unknown" elements can be added to a complex type in various positions and what the parser should do if it hits any of those elements

您可以使用XML 1.1中的一个新特性“Open Content”。简而言之,允许您指定额外的“未知”元素可以添加到各种位置的复杂类型中,如果解析器碰到这些元素中的任何一个,它应该做什么。

Using XML 1.1, your complex type would become:

使用XML 1.1,您的复杂类型将变成:

<xs:element name="root" type="root" />
<xs:complexType name="root"> 
  <xs:openContent mode="interleave">
    <xs:any namespace="##any" processContents="skip"/>
  </xs:openContent>

  <xs:sequence> 
    <xs:element name="node" type="xs:string"/> 
  </xs:sequence> 
</xs:complexType>

If you have a lot of complex types, you can also set a "default" open content mode at the top of your schema:

如果您有很多复杂类型,您还可以在模式的顶部设置“默认”开放内容模式:

<xs:schema ...>
  <xs:defaultOpenContent mode="interleave">
    <xs:any namespace="##any" processContents="skip"/>
  </xs:defaultOpenContent>

  ...
</xs:schema>

The W3C spec for Open Content can be found at http://www.w3.org/TR/xmlschema11-1/#oc and there's a good writeup of this at http://www.ibm.com/developerworks/library/x-xml11pt3/#N102BA.

开放内容的W3C规范可以在http://www.w3.org/TR/xmlschema11-1/#oc中找到,并且在http://www.ibm.com/developerworks/library/xxml11pt3 /#N102BA中有一个很好的注释。

Unfortunately, .NET doesn't support XML 1.1 as of yet I can't find any free XML 1.1 processors - but a couple of paid-for options are:

不幸的是,.NET目前还不支持XML 1.1,我还找不到任何免费的XML 1.1处理器——但是有几个付费选项是:

#1


3  

In case your not already done with this, you might try the following:

如果你还没有完成这项工作,你可以试试以下方法:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root" type="root"></xs:element>
  <xs:complexType name="root">
    <xs:sequence>
      <xs:any maxOccurs="2" minOccurs="0" processContents="skip"/>
      <xs:element name="node" type="xs:string"/>
      <xs:any maxOccurs="2" minOccurs="0" processContents="skip"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Under Linux this works fine with xmllint using libxml version 20706.

在Linux下,使用libxml版本20706使用xmllint可以正常工作。

#2


2  

Conclusion:

结论:

This is not possible with XSD. All the approaches I was trying to achieve the requirement were named as "ambiguous" by validation-tools, accompanying bunch of errors.

对于XSD,这是不可能的。我试图实现需求的所有方法都被验证工具命名为“歧义”,并伴有大量错误。

#3


1  

Maybe its is possible to use namespaces:

也许使用名称空间是可能的:

<xs:element name="root" type="root"></xs:element> 
  <xs:complexType name="root"> 
    <xs:sequence> 
      <xs:any maxOccurs="2" minOccurs="0" namespace="http://ns1.com" /> 
      <xs:element name="node" type="xs:string"/> 
      <xs:any maxOccurs="2" minOccurs="0" namespace="http://ns2.com"/> 
    </xs:sequence> 
  </xs:complexType>

This will probably validate.

这可能会验证。

#4


1  

I faced the same problem.

我也遇到了同样的问题。

Since I called the validation from .NET; I decided to suppress the specific validation error in ValidationEventHandler as a workaround. It worked for me.

因为我从。net调用了验证;我决定将ValidationEventHandler中的特定验证错误作为解决方案加以抑制。它为我工作。

    private void ValidationEventHandler(object sender, ValidationEventArgs e)
    {
        switch (e.Severity)
        {
            case XmlSeverityType.Warning:
                // Processing warnings
                break;
            case XmlSeverityType.Error:
                if (IgnoreUnknownTags
                    && e.Exception is XmlSchemaValidationException
                    && new Regex(
                        @"The element '.*' has invalid child element '.*'\."
                        + @" List of possible elements expected:'.*'\.")
                       .IsMatch(e.Exception.Message))
                {
                    return;
                }
                // Processing errors
                break;
            default:
                throw new InvalidEnumArgumentException("Severity should be one of the valid values");
        }
    }

It is important that Thread.CurrentUICulture must be set to English or CultureInfo.InvariantCulture for the current thread for this to work.

这条线很重要。当前文化必须设置为英语或文化信息。当前线程的不变性使其工作。

#5


0  

You could make use of a new feature in XML 1.1 called "Open Content". In short, allows you to specify that additional "unknown" elements can be added to a complex type in various positions and what the parser should do if it hits any of those elements

您可以使用XML 1.1中的一个新特性“Open Content”。简而言之,允许您指定额外的“未知”元素可以添加到各种位置的复杂类型中,如果解析器碰到这些元素中的任何一个,它应该做什么。

Using XML 1.1, your complex type would become:

使用XML 1.1,您的复杂类型将变成:

<xs:element name="root" type="root" />
<xs:complexType name="root"> 
  <xs:openContent mode="interleave">
    <xs:any namespace="##any" processContents="skip"/>
  </xs:openContent>

  <xs:sequence> 
    <xs:element name="node" type="xs:string"/> 
  </xs:sequence> 
</xs:complexType>

If you have a lot of complex types, you can also set a "default" open content mode at the top of your schema:

如果您有很多复杂类型,您还可以在模式的顶部设置“默认”开放内容模式:

<xs:schema ...>
  <xs:defaultOpenContent mode="interleave">
    <xs:any namespace="##any" processContents="skip"/>
  </xs:defaultOpenContent>

  ...
</xs:schema>

The W3C spec for Open Content can be found at http://www.w3.org/TR/xmlschema11-1/#oc and there's a good writeup of this at http://www.ibm.com/developerworks/library/x-xml11pt3/#N102BA.

开放内容的W3C规范可以在http://www.w3.org/TR/xmlschema11-1/#oc中找到,并且在http://www.ibm.com/developerworks/library/xxml11pt3 /#N102BA中有一个很好的注释。

Unfortunately, .NET doesn't support XML 1.1 as of yet I can't find any free XML 1.1 processors - but a couple of paid-for options are:

不幸的是,.NET目前还不支持XML 1.1,我还找不到任何免费的XML 1.1处理器——但是有几个付费选项是: