I have a 2 part question:
我有两个问题:
-
How can I get the
regex
expression of anXSD
facet and then use it to determine if a string matches the restriction? In my mind, this is "How do I convert XML Schema regex to .NETRegex
", but I'm open for suggestions if you have another way for me to do it other than converting the expression.如何获取XSD facet的regex表达式,然后使用它来确定字符串是否匹配该限制?在我看来,这是“如何将XML模式regex转换为。net regex”,但如果您有其他方法可以将表达式转换为其他方式,那么我愿意接受建议。
-
If the test (#1) fails, how can I use the
XSD
patternregex
to automatically create a string which does satisfy the constraint?如果测试(#1)失败,如何使用XSD模式regex自动创建满足约束的字符串?
2 个解决方案
#1
2
-
XmlSchemaDatatype.ParseValue is your answer. Assuming the associated simple type has more facets and you only want to validate against the pattern one(s), then you have to simply find the enumeration facet in the XmlSchemaSimpleTypeRestriction.Facets, use a copy of that to create a new XmlSchemaSimpleType, with a new XmlSchemaSimpleTypeRestriction Content and new pattern facet(s) using the values you scooped above. Then using this newly created simple type, invoke XmlSchemaDatatype.ParseValue.
XmlSchemaDatatype。ParseValue就是你的答案。假设关联的简单类型有更多的方面,并且您只想针对模式1进行验证,那么您只需在XmlSchemaSimpleTypeRestriction中找到枚举方面。facet,使用它的一个副本来创建一个新的XmlSchemaSimpleType,其中包含一个新的XmlSchemaSimpleTypeRestriction内容,以及使用前面所获取的值创建一个新的模式facet。然后使用这个新创建的简单类型,调用XmlSchemaDatatype.ParseValue。
I would advise against your suggestion in the comment, since the regex "dialects" are different.
我反对你在评论中提出的建议,因为regex“方言”是不同的。
-
I am not aware of such a thing, available for free or otherwise. I am sure it can be done but I never found something that would actually work, when I needed it myself. If you do find one, please share.
我不知道有这样的东西,免费或其他。我相信这是可以做到的,但当我自己需要的时候,我从来没有找到真正有用的东西。如果你找到了,请分享。
#2
1
It is not too difficult to convert a XML Schema regex to a .NET regex.
将XML模式regex转换为. net regex并不太难。
Basically you need to replace few patterns such as \c and \D with by their .NET alternatives such as \p{_xmlC} and \P{_xmlD}.
基本上,您需要用它们的。net替代方案(如\p{_xmlC}和\p{_xmlD})替换一些模式,如\c和\D。
Also you need to wrap expression in ^ and $ markers.
你也需要包装表达^和$标记。
.NET implements this in method Preprocess in https://github.com/Microsoft/referencesource/blob/master/System.Xml/System/Xml/Schema/FacetChecker.cs
.NET在https://github.com/microsoft/referencesource/blob/master/system.xml/system.xml/system/xml/schema/facetchecker.cs中实现了这一点
If you decide to copy-paste the implementation, be careful, though.
如果您决定复制粘贴实现,那么请小心。
You need to replace loop
您需要替换循环
for (int position = 0; position < length - 2; position ++)
for (int position = 0;位置 <长度- 2;位置+ +)< p>
with
与
for (int position = 0; position < length - 1; position ++)
for (int position = 0;位置 <长度- 1;位置+ +)< p>
because for optimization reasons Preprocess assumes the input expression is enclosed in parentheses.
因为出于优化原因,Preprocess假设输入表达式包含在圆括号中。
#1
2
-
XmlSchemaDatatype.ParseValue is your answer. Assuming the associated simple type has more facets and you only want to validate against the pattern one(s), then you have to simply find the enumeration facet in the XmlSchemaSimpleTypeRestriction.Facets, use a copy of that to create a new XmlSchemaSimpleType, with a new XmlSchemaSimpleTypeRestriction Content and new pattern facet(s) using the values you scooped above. Then using this newly created simple type, invoke XmlSchemaDatatype.ParseValue.
XmlSchemaDatatype。ParseValue就是你的答案。假设关联的简单类型有更多的方面,并且您只想针对模式1进行验证,那么您只需在XmlSchemaSimpleTypeRestriction中找到枚举方面。facet,使用它的一个副本来创建一个新的XmlSchemaSimpleType,其中包含一个新的XmlSchemaSimpleTypeRestriction内容,以及使用前面所获取的值创建一个新的模式facet。然后使用这个新创建的简单类型,调用XmlSchemaDatatype.ParseValue。
I would advise against your suggestion in the comment, since the regex "dialects" are different.
我反对你在评论中提出的建议,因为regex“方言”是不同的。
-
I am not aware of such a thing, available for free or otherwise. I am sure it can be done but I never found something that would actually work, when I needed it myself. If you do find one, please share.
我不知道有这样的东西,免费或其他。我相信这是可以做到的,但当我自己需要的时候,我从来没有找到真正有用的东西。如果你找到了,请分享。
#2
1
It is not too difficult to convert a XML Schema regex to a .NET regex.
将XML模式regex转换为. net regex并不太难。
Basically you need to replace few patterns such as \c and \D with by their .NET alternatives such as \p{_xmlC} and \P{_xmlD}.
基本上,您需要用它们的。net替代方案(如\p{_xmlC}和\p{_xmlD})替换一些模式,如\c和\D。
Also you need to wrap expression in ^ and $ markers.
你也需要包装表达^和$标记。
.NET implements this in method Preprocess in https://github.com/Microsoft/referencesource/blob/master/System.Xml/System/Xml/Schema/FacetChecker.cs
.NET在https://github.com/microsoft/referencesource/blob/master/system.xml/system.xml/system/xml/schema/facetchecker.cs中实现了这一点
If you decide to copy-paste the implementation, be careful, though.
如果您决定复制粘贴实现,那么请小心。
You need to replace loop
您需要替换循环
for (int position = 0; position < length - 2; position ++)
for (int position = 0;位置 <长度- 2;位置+ +)< p>
with
与
for (int position = 0; position < length - 1; position ++)
for (int position = 0;位置 <长度- 1;位置+ +)< p>
because for optimization reasons Preprocess assumes the input expression is enclosed in parentheses.
因为出于优化原因,Preprocess假设输入表达式包含在圆括号中。