A
aevans1108
expanding this message to microsoft.public.dotnet.xml
Greetings
Please direct me to the right group if this is an inappropriate place
to post this question. Thanks.
I want to format a numeric value according to an arbitrary regular
expression.
Background:
I have an XML schema that I have no control over. It is filled with
simpleTypes with restrictions that include xsd
attern elements, eg:
<xsd:restriction base="xsd:float">
<xsd:minInclusive value="0.000" />
<xsd:maxInclusive value="10.000" />
<xsd
attern value="\d{1,2}\.\d{3}" />
</xsd:restriction>
These patterns and BaseTypes vary widely.
I am dynamically creating an XmlDocument at runtime and validating it
against the schema. Each XmlDocumentElement.InnerText property is
populated from a data structure that contains an object reference that
points to a value of the appropriate type. That is: The object
references may point to a float or to a string or whatever is called
for, depending on the BaseType of the corresponding simpleType.
(Usings strings for everything is not an option.)
The problem is that in order to create the XmlDocument, I have to
convert an object that might actually be a float or an int in to a
formatted string.
I can't just go
myElement.InnerText = Convert.ToString( Convert.ToDouble( myObject ) );
because myElement.InnerText will then contain strings that look like
this
1.000 ==> "1"
1.050 ==> "1.05"
which cannot be guaranteed to satisfy the schema. It certainly doesn't
satisfy the one above. In the case of that schema, I would obviously
want to have conversions like this:
1.000 ==> "1.000"
1.050 ==> "1.050"
Yes, I know I can easily write code to do this for a single regular
expression, but the problem is that there is no guarantee what the
xsd
attern is going to be. Hard coding something that uses three
decimal places just won't do.
Here's what I would like to do:
myElement.InnerText = FormatAccordingToRegEx( Convert.ToDouble(
myObject ), myXsdPattern );
Remember, myXsdPattern could be anything, so I can't do something like
Regex.Replace( Convert.ToString( Convert.ToDouble( myObject ) ),
myXsdPattern, "${1},${2}.${3}" ); // not sure about syntax here
That won't work if myXsdPattern = "\d{1,}.\d{3}" or "\d{1,}.\d{1}" or
"-{0,1}\d{1,2}.\d{1}" (to list a few examples); and even if it did, I
think I would need to have access to the schema so I could interject
parentheses in to the regular expressions/xsd
atterns. As I said, I
have no control over the schema. (Although, the ultimate solution may
indeed require me to read the xsd
attern in, insert parentheses
according to some algorithm and then do something like the code
fragment above. I don't know.)
Now, I'm pretty sure that xsd
attern will only be used with numeric
values like xsd:float and xsd:int in the schema. I don't think it
would be possible to do this with an arbitrary string anyway because in
many cases, several formatted outputs are possible from one input... so
I would think this simplifies the problem a bit. (Several outputs are
possible from one input with numbers as well, but they all mean the
same thing -- leading zeros and commas are pretty irrelevant. A number
is still a number.)
I hope I'm being clear. I'm grammatically challenged today.
I know this is a bit backward, but I'm though I'd check and see if
anyone has already written code to do this. I will be grateful for any
and all suggestions -- even guesses about what to try.
Thanks in advance.
Tony
Greetings
Please direct me to the right group if this is an inappropriate place
to post this question. Thanks.
I want to format a numeric value according to an arbitrary regular
expression.
Background:
I have an XML schema that I have no control over. It is filled with
simpleTypes with restrictions that include xsd

<xsd:restriction base="xsd:float">
<xsd:minInclusive value="0.000" />
<xsd:maxInclusive value="10.000" />
<xsd

</xsd:restriction>
These patterns and BaseTypes vary widely.
I am dynamically creating an XmlDocument at runtime and validating it
against the schema. Each XmlDocumentElement.InnerText property is
populated from a data structure that contains an object reference that
points to a value of the appropriate type. That is: The object
references may point to a float or to a string or whatever is called
for, depending on the BaseType of the corresponding simpleType.
(Usings strings for everything is not an option.)
The problem is that in order to create the XmlDocument, I have to
convert an object that might actually be a float or an int in to a
formatted string.
I can't just go
myElement.InnerText = Convert.ToString( Convert.ToDouble( myObject ) );
because myElement.InnerText will then contain strings that look like
this
1.000 ==> "1"
1.050 ==> "1.05"
which cannot be guaranteed to satisfy the schema. It certainly doesn't
satisfy the one above. In the case of that schema, I would obviously
want to have conversions like this:
1.000 ==> "1.000"
1.050 ==> "1.050"
Yes, I know I can easily write code to do this for a single regular
expression, but the problem is that there is no guarantee what the
xsd

decimal places just won't do.
Here's what I would like to do:
myElement.InnerText = FormatAccordingToRegEx( Convert.ToDouble(
myObject ), myXsdPattern );
Remember, myXsdPattern could be anything, so I can't do something like
Regex.Replace( Convert.ToString( Convert.ToDouble( myObject ) ),
myXsdPattern, "${1},${2}.${3}" ); // not sure about syntax here
That won't work if myXsdPattern = "\d{1,}.\d{3}" or "\d{1,}.\d{1}" or
"-{0,1}\d{1,2}.\d{1}" (to list a few examples); and even if it did, I
think I would need to have access to the schema so I could interject
parentheses in to the regular expressions/xsd

have no control over the schema. (Although, the ultimate solution may
indeed require me to read the xsd

according to some algorithm and then do something like the code
fragment above. I don't know.)
Now, I'm pretty sure that xsd

values like xsd:float and xsd:int in the schema. I don't think it
would be possible to do this with an arbitrary string anyway because in
many cases, several formatted outputs are possible from one input... so
I would think this simplifies the problem a bit. (Several outputs are
possible from one input with numbers as well, but they all mean the
same thing -- leading zeros and commas are pretty irrelevant. A number
is still a number.)
I hope I'm being clear. I'm grammatically challenged today.
I know this is a bit backward, but I'm though I'd check and see if
anyone has already written code to do this. I will be grateful for any
and all suggestions -- even guesses about what to try.
Thanks in advance.
Tony