validating individual element names?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Developer

    validating individual element names?

    Hello,

    Is there a way to validate an xml fragment? For example, I'd like to know
    whether this tag is legal, and if not, what's wrong:

    <complexType name="CIty Grid">

    Thanks.


  • Martin Honnen

    #2
    Re: validating individual element names?



    Developer wrote:

    [color=blue]
    > Is there a way to validate an xml fragment? For example, I'd like to know
    > whether this tag is legal, and if not, what's wrong:
    >
    > <complexType name="CIty Grid">[/color]

    Even an XML fragment (not sure whether that is defined anywhere) needs
    to have a matching closing tag for that start tag.

    Also it is not clear what you have in mind, are you actually writing an
    XML schema and want to check that? Or is that complexType just an
    example of an XML element name?

    If you want to create XML then you can use XmlTextWriter, it has a
    method WriteName that takes a string argument and checks whether that is
    an allowed XML name, if not it throws an exception.



    --

    Martin Honnen


    Comment

    • Zafar Abbas [MSFT]

      #3
      Re: validating individual element names?

      Read up this section of MSDN describing fragment validation using
      XmlValidatingRe ader:


      "Developer" <wanderer@mapin fo.nope.com> wrote in message
      news:OG3W0xifEH A.3476@tk2msftn gp13.phx.gbl...[color=blue]
      > Hello,
      >
      > Is there a way to validate an xml fragment? For example, I'd like to know
      > whether this tag is legal, and if not, what's wrong:
      >
      > <complexType name="CIty Grid">
      >
      > Thanks.
      >
      >[/color]


      Comment

      • Developer

        #4
        Re: validating individual element names?

        Thanks for the link. Is there an msxml equivalent, since I can't use the
        ..Net framework?

        "Zafar Abbas [MSFT]" <zafara@microso ft.com> wrote in message
        news:OU9uiZjfEH A.632@TK2MSFTNG P12.phx.gbl...[color=blue]
        > Read up this section of MSDN describing fragment validation using
        > XmlValidatingRe ader:
        >[/color]
        http://msdn.microsoft.com/library/de...tingReader.asp[color=blue]
        >
        > "Developer" <wanderer@mapin fo.nope.com> wrote in message
        > news:OG3W0xifEH A.3476@tk2msftn gp13.phx.gbl...[color=green]
        > > Hello,
        > >
        > > Is there a way to validate an xml fragment? For example, I'd like to[/color][/color]
        know[color=blue][color=green]
        > > whether this tag is legal, and if not, what's wrong:
        > >
        > > <complexType name="CIty Grid">
        > >
        > > Thanks.
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Developer

          #5
          Re: validating individual element names?

          I'm getting a well-formed schema from a server. Since it is well-formed, it
          loads into an XmlDocument. I parse the element names, and need to detect
          invalid ones.
          I need to do this with msxml, if possible, since I can't use the .Net
          framework.

          Thanks for the reply.

          "Martin Honnen" <mahotrash@yaho o.de> wrote in message
          news:uWR4aXjfEH A.3700@TK2MSFTN GP12.phx.gbl...[color=blue]
          >
          >
          > Developer wrote:
          >
          >[color=green]
          > > Is there a way to validate an xml fragment? For example, I'd like to[/color][/color]
          know[color=blue][color=green]
          > > whether this tag is legal, and if not, what's wrong:
          > >
          > > <complexType name="CIty Grid">[/color]
          >
          > Even an XML fragment (not sure whether that is defined anywhere) needs
          > to have a matching closing tag for that start tag.
          >
          > Also it is not clear what you have in mind, are you actually writing an
          > XML schema and want to check that? Or is that complexType just an
          > example of an XML element name?
          >
          > If you want to create XML then you can use XmlTextWriter, it has a
          > method WriteName that takes a string argument and checks whether that is
          > an allowed XML name, if not it throws an exception.
          >
          >
          >
          > --
          >
          > Martin Honnen
          > http://JavaScript.FAQTs.com/
          >[/color]


          Comment

          • Martin Honnen

            #6
            Re: validating individual element names?



            Developer wrote:
            [color=blue]
            > I'm getting a well-formed schema from a server. Since it is well-formed, it
            > loads into an XmlDocument. I parse the element names, and need to detect
            > invalid ones.
            > I need to do this with msxml, if possible, since I can't use the .Net
            > framework.[/color]

            Yes, you can validate schemas by loading them into a schema cache, here
            is an example:

            var xmlSchemaCache = new ActiveXObject(' Msxml2.XMLSchem aCache.4.0');
            try {
            xmlSchemaCache. add('', 'test20040809Xs d.xml');
            }
            catch (e) {
            alert(e.message );
            }

            If the example schema test20040809Xsd .xml is as follows:

            <?xml version="1.0" encoding="UTF-8"?>
            <xs:schema
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            version="1.0">

            <xs:element name="element name" type="xs:string " />

            </xs:schema>

            then the error message is

            PathToFile/javascript/test20040809Xsd .xml#/schema[1]/element[position()
            = 1 and @name = 'element name']
            Error parsing 'element name' as NCName datatype.
            A name contained an invalid character.


            --

            Martin Honnen


            Comment

            • Developer

              #7
              Re: validating individual element names?

              Thank you, Martin, I discovered that technique. Unfortunately, it is too
              strict for what I need to do.
              For example, it gives me an error on something like this:
              <xsd:import namespace="http ://www.opengis.net/gml"
              schemaLocation= "http://www.opengis.net/namespaces/gml/core/feature.xsd"/>
              For my purposes, I don't care about that error, and once that problem is
              detected, processing stops. An invalid name in a start tag, however, is
              something I would like to know about.
              So, I'm looking for a more granular approach: is this element name valid?
              Is this start tag valid? This end tag?

              Thanks again.


              "Martin Honnen" <mahotrash@yaho o.de> wrote in message
              news:uxP62tjfEH A.704@TK2MSFTNG P09.phx.gbl...[color=blue]
              >
              >
              > Developer wrote:
              >[color=green]
              > > I'm getting a well-formed schema from a server. Since it is[/color][/color]
              well-formed, it[color=blue][color=green]
              > > loads into an XmlDocument. I parse the element names, and need to[/color][/color]
              detect[color=blue][color=green]
              > > invalid ones.
              > > I need to do this with msxml, if possible, since I can't use the .Net
              > > framework.[/color]
              >
              > Yes, you can validate schemas by loading them into a schema cache, here
              > is an example:
              >
              > var xmlSchemaCache = new ActiveXObject(' Msxml2.XMLSchem aCache.4.0');
              > try {
              > xmlSchemaCache. add('', 'test20040809Xs d.xml');
              > }
              > catch (e) {
              > alert(e.message );
              > }
              >
              > If the example schema test20040809Xsd .xml is as follows:
              >
              > <?xml version="1.0" encoding="UTF-8"?>
              > <xs:schema
              > xmlns:xs="http://www.w3.org/2001/XMLSchema"
              > version="1.0">
              >
              > <xs:element name="element name" type="xs:string " />
              >
              > </xs:schema>
              >
              > then the error message is
              >
              > PathToFile/javascript/test20040809Xsd .xml#/schema[1]/element[position()
              > = 1 and @name = 'element name']
              > Error parsing 'element name' as NCName datatype.
              > A name contained an invalid character.
              >
              >
              > --
              >
              > Martin Honnen
              > http://JavaScript.FAQTs.com/
              >[/color]


              Comment

              • Zafar Abbas [MSFT]

                #8
                Re: validating individual element names?

                MSXML does not let you selectively ignore validation errors. You will have
                to use the .Net framework for that where you can hook up a validation event
                handler and choose to proceed on the selective errors.



                "Developer" <wanderer@mapin fo.nope.com> wrote in message
                news:%23KxdIvof EHA.3320@TK2MSF TNGP11.phx.gbl. ..[color=blue]
                > Thank you, Martin, I discovered that technique. Unfortunately, it is too
                > strict for what I need to do.
                > For example, it gives me an error on something like this:
                > <xsd:import namespace="http ://www.opengis.net/gml"
                > schemaLocation= "http://www.opengis.net/namespaces/gml/core/feature.xsd"/>
                > For my purposes, I don't care about that error, and once that problem is
                > detected, processing stops. An invalid name in a start tag, however, is
                > something I would like to know about.
                > So, I'm looking for a more granular approach: is this element name valid?
                > Is this start tag valid? This end tag?
                >
                > Thanks again.
                >
                >
                > "Martin Honnen" <mahotrash@yaho o.de> wrote in message
                > news:uxP62tjfEH A.704@TK2MSFTNG P09.phx.gbl...[color=green]
                > >
                > >
                > > Developer wrote:
                > >[color=darkred]
                > > > I'm getting a well-formed schema from a server. Since it is[/color][/color]
                > well-formed, it[color=green][color=darkred]
                > > > loads into an XmlDocument. I parse the element names, and need to[/color][/color]
                > detect[color=green][color=darkred]
                > > > invalid ones.
                > > > I need to do this with msxml, if possible, since I can't use the .Net
                > > > framework.[/color]
                > >
                > > Yes, you can validate schemas by loading them into a schema cache, here
                > > is an example:
                > >
                > > var xmlSchemaCache = new ActiveXObject(' Msxml2.XMLSchem aCache.4.0');
                > > try {
                > > xmlSchemaCache. add('', 'test20040809Xs d.xml');
                > > }
                > > catch (e) {
                > > alert(e.message );
                > > }
                > >
                > > If the example schema test20040809Xsd .xml is as follows:
                > >
                > > <?xml version="1.0" encoding="UTF-8"?>
                > > <xs:schema
                > > xmlns:xs="http://www.w3.org/2001/XMLSchema"
                > > version="1.0">
                > >
                > > <xs:element name="element name" type="xs:string " />
                > >
                > > </xs:schema>
                > >
                > > then the error message is
                > >
                > > PathToFile/javascript/test20040809Xsd .xml#/schema[1]/element[position()
                > > = 1 and @name = 'element name']
                > > Error parsing 'element name' as NCName datatype.
                > > A name contained an invalid character.
                > >
                > >
                > > --
                > >
                > > Martin Honnen
                > > http://JavaScript.FAQTs.com/
                > >[/color]
                >
                >[/color]


                Comment

                • Developer

                  #9
                  Re: validating individual element names?

                  Thanks, Zafar, that seems to answer my question -- I can't do it with msxml
                  4.

                  "Zafar Abbas [MSFT]" <zafara@microso ft.com> wrote in message
                  news:OLwvD1xfEH A.140@TK2MSFTNG P12.phx.gbl...[color=blue]
                  > MSXML does not let you selectively ignore validation errors. You will have
                  > to use the .Net framework for that where you can hook up a validation[/color]
                  event[color=blue]
                  > handler and choose to proceed on the selective errors.
                  >
                  >
                  >[/color]


                  Comment

                  Working...