XML namespaces and prefix

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

    XML namespaces and prefix

    I am trying to use xpath queries on an xmldocument passed to my web service.
    The document nodes have namespace prefixes. How can I programatically
    create an xpath query and run it assuming that the namespace and prefix
    could potentially change?

    Here is an example of my xml (from an infopath form in this case, but could
    be from any application)

    <?xml version="1.0" encoding="UTF-8"?>
    <?mso-infoPathSolutio n solutionVersion ="1.0.0.52" productVersion= "11.0.6357"
    PIVersion="1.0. 0.0"
    href="http://testmachine/infopathforms/DocumentLibrary Test.xsn"
    name="urn:schem as-microsoft-com:office:info path:DocumentLi braryTest:-myXSD-2005-07-28T21-22-28"
    ?><?mso-application progid="InfoPat h.Document"?>
    <my:Form xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xmlns:my="http://schemas.microso ft.com/office/infopath/2003/myXSD/2005-07-28T21:22:28"
    xmlns:xd="http://schemas.microso ft.com/office/infopath/2003"
    xml:lang="en-us">
    <my:Header>
    <my:PHN>1234</my:PHN>
    </my:Header>
    <my:Footer>
    <my:DateComplet ed>2005-08-04</my:DateComplete d>
    <my:CompletedBy >123</my:CompletedBy>
    </my:Footer>
    <my:Description >
    <my:Text></my:Text>
    </my:Description>
    <my:Extra>
    <my:Active>fals e</my:Active>
    <my:ExtraText ></my:ExtraText>
    </my:Extra>
    </my:Form>


  • Marvin Smit

    #2
    Re: XML namespaces and prefix

    Hi,

    Ehh.. if you say "assuming that the namespace and prefix could
    potentially change" it is a strange construct. If a namespace would
    'suddenly change', all clients would have problems. This is usualy a
    'versioning' step that happened. My advise is ALWAYS change the
    namespace uri, if the schema constructs change. But, maintain the
    precious versions of the schema. Do not overwrite the old one.


    For this example you should do something like:
    (From the top of my head, forgive me any typo's)

    XmlNamespaceMan ager xNS = new XmlNamespaceMan ager( new NameTable () );
    xNS.AddNamespac e( "prefix", "namespaceU ri" );

    XmlDocument xDoc = new XmlDocument();
    xDoc.Load( @"C:\temp\somex ml.xml" );

    XmlNodeList xNL = xDoc.DocumentEl ement.SelectNod es (
    "prefix:MainRoo t/prefix:SomeChil d" );


    Hope this helps,

    Marvin Smit.


    On Fri, 29 Jul 2005 11:06:57 -0700, "Jeremy Chapman" <me@here.com>
    wrote:
    [color=blue]
    >I am trying to use xpath queries on an xmldocument passed to my web service.
    >The document nodes have namespace prefixes. How can I programatically
    >create an xpath query and run it assuming that the namespace and prefix
    >could potentially change?
    >
    >Here is an example of my xml (from an infopath form in this case, but could
    >be from any application)
    >
    ><?xml version="1.0" encoding="UTF-8"?>
    ><?mso-infoPathSolutio n solutionVersion ="1.0.0.52" productVersion= "11.0.6357"
    >PIVersion="1.0 .0.0"
    >href="http://testmachine/infopathforms/DocumentLibrary Test.xsn"
    >name="urn:sche mas-microsoft-com:office:info path:DocumentLi braryTest:-myXSD-2005-07-28T21-22-28"
    >?><?mso-application progid="InfoPat h.Document"?>
    ><my:Form xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    >xmlns:my="http ://schemas.microso ft.com/office/infopath/2003/myXSD/2005-07-28T21:22:28"
    >xmlns:xd="http ://schemas.microso ft.com/office/infopath/2003"
    >xml:lang="en-us">
    > <my:Header>
    > <my:PHN>1234</my:PHN>
    > </my:Header>
    > <my:Footer>
    > <my:DateComplet ed>2005-08-04</my:DateComplete d>
    > <my:CompletedBy >123</my:CompletedBy>
    > </my:Footer>
    > <my:Description >
    > <my:Text></my:Text>
    > </my:Description>
    > <my:Extra>
    > <my:Active>fals e</my:Active>
    > <my:ExtraText ></my:ExtraText>
    > </my:Extra>
    ></my:Form>
    >[/color]

    Comment

    Working...