1

これがどれほど実行可能かはわかりませんが、XSDのデータ型に取り組んでおり、私がやろうとしていることの1つは、姓にハイフンを使用できるようにデータ型を拡張することです。したがって、これはとに一致する必要がSmith FryありSafran-Foerます。さらに、チェックする文字列の長さを100文字(または101文字)以下に制限したいと思います。私の正規表現は元々次のとおりでした。

 <xsd:pattern value="[a-zA-Z ]{0,100}"/>

これで、これを分割して、次のように両側に50文字を任意に許可することができることがわかりました。

 <xsd:pattern value="[a-zA-Z ]{0,50}(\-)?[a-zA-Z ]{0,50}"/>

しかし、それは不潔に思えます。次のようなことをする方法はありますか?

 <xsd:pattern value="[a-zA-Z (\-)?]{0,100}"/>

私が探しているものを尋ねる別の方法は、「1つ以下のハイフンを含む0から100までの文字列を一致させる」です。

ありがとう!

4

1 に答える 1

2

'Match a string of characters between 0 and 100 long with no more than 1 hyphen in it'これは、いくつかの追加の制約に加えてスイングです。

  • 空白を許可する
  • ハイフンで開始または終了することはできません

XSD 正規表現でサポートされている構文を考慮すると、パターンで最大長を実行できるとは思いません。ただし、maxLength ファセットと組み合わせるのは簡単です。

これは XSD です。

<?xml version="1.0" encoding="utf-8" ?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="last-name">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:maxLength value="100"/>
                <xsd:pattern value="[a-zA-Z ]+\-?[a-zA-Z ]+"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>
</xsd:schema>

空白のみで囲まれたハイフンを禁止するなど、パターンをさらに洗練することができます。

有効な XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<last-name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">last - name</last-name>

無効な XML (ハイフンが多すぎる) とメッセージ:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<last-name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">l-ast - name</last-name>

検証エラー:

Error occurred while loading [], line 3 position 121 The 'http://tempuri.org/XMLSchema.xsd:last-name' element is invalid - The value 'l-ast - name' is invalid according to its datatype 'String' - The Pattern constraint failed.

無効な XML (maxLength=14 を使用したテストでは最大長よりも長い) とメッセージ:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<last-name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">last - name that is longer</last-name>

検証エラー:

Error occurred while loading [], line 3 position 135 The 'http://tempuri.org/XMLSchema.xsd:last-name' element is invalid - The value 'last - name that is longer' is invalid according to its datatype 'String' - The actual length is greater than the MaxLength value.

于 2012-03-28T02:27:17.557 に答える