【问题标题】:c# XDocument Xsd pattern validation with character $c# XDocument Xsd 模式验证与字符 $
【发布时间】:2012-02-14 09:25:45
【问题描述】:

我在使用 c# XDocument XSD 验证时遇到问题。

Xml Spy 对以下文件进行了很好的验证,但 .Net (c#) 对以下文件进行了很好的验证

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Simple.xsd">
    <Var Name="$Toto"/>
</Root>

架构

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="Root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Var">
                    <xs:complexType>
                        <xs:attribute name="Name" type="ST_VariableIdentifier" use="required"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
        <xs:simpleType name="ST_VariableIdentifier">
        <xs:restriction base="xs:string">
            <xs:pattern value="$[a-z,A-Z]*"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

一个想法?

【问题讨论】:

  • 这会受益于 C# 标签吗?
  • 这是 C# 中的一个错误,有一个解决方法:使用 [$]。不要费心尝试诸如字符引用之类的东西,例如$ ,由于某种原因,C#在这里很挑剔...

标签: validation xsd design-patterns


【解决方案1】:

这应该可行!

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Var">
          <xs:complexType>
            <xs:attribute name="Name" type="ST_VariableIdentifier" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="ST_VariableIdentifier">
    <xs:restriction base="xs:string">
      <xs:pattern value="[$][a-z,A-Z]*"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

【讨论】:

    【解决方案2】:

    只是为了添加到现有答案。这实际上是 W3C 规范的 .Net 实现中的一个已知“错误”(在 Connect 上已确认 here,但 不会修复)。

    MSDN 提供了更多信息 (here) 以及上述解决方法。

    万维网联盟 (W3C) 的 System.Xml 实现 XML Schemas 的建议使用 RegEx 类来执行 正则表达式的模式匹配。在某些情况下,行为 W3C 推荐的行为不同于 RegEx 类行为。这 以下是 System.Xml 实现的已知情况 模式匹配不同于 W3C 规范:

    根据 W3C for XML Schema 规范,美元符号 ($) 应被视为常规字符。但是,System.Xml 验证将 xs:pattern 中的美元符号解释为 线尾锚。可能的解决方法是将 $ 替换为 [$]。 如果您的模式已经在括号中,例如 [abc$],则不是 有必要进行任何更改。

    【讨论】:

      猜你喜欢
      • 2014-07-13
      • 2019-11-03
      • 1970-01-01
      • 2023-03-27
      • 2010-11-26
      • 2012-12-13
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多