Link to home
Start Free TrialLog in
Avatar of brettr
brettr

asked on

Where is this XPATH?

I'm using c# .NET 2.0 in VS.NET 2005.  This XPATH seems to find a matching node in the XML below:

//Directory[@name='abc']//Directory[@name='myapp']//Directory[@name='images']

--using this XML

<Directory name="abc" xmlns="">

  <Directory name="myapp">

    <Directory name="blog">

      <Directory name="wp-admin">

        <Directory name="images" />

        <Directory name="import" />

      </Directory>

      <Directory name="wp-content">

        <Directory name="plugins">

          <Directory name="akismet" />

        </Directory>

        <Directory name="themes">

          <Directory name="classic" />

          <Directory name="default">

            <Directory name="images" />

          </Directory>

          <Directory name="glossyblue-advanced-1-1">

            <Directory name="images" />

          </Directory>

        </Directory>

      </Directory>

      <Directory name="wp-includes">

        <Directory name="images">

          <Directory name="smilies" />

        </Directory>

        <Directory name="js">

          <Directory name="tinymce">

            <Directory name="langs" />

            <Directory name="plugins">

              <Directory name="autosave">

                <Directory name="langs" />

              </Directory>

              <Directory name="directionality">

                <Directory name="images" />

                <Directory name="langs" />

              </Directory>

              <Directory name="inlinepopups">

                <Directory name="css" />

                <Directory name="images" />

                <Directory name="jscripts" />

              </Directory>

              <Directory name="wordpress">

                <Directory name="images" />

                <Directory name="langs" />

              </Directory>

              <Directory name="wphelp">

                <Directory name="images" />

                <Directory name="langs" />

              </Directory>

            </Directory>

            <Directory name="themes">

              <Directory name="advanced">

                <Directory name="css" />

                <Directory name="images">

                  <Directory name="xp" />

                </Directory>

                <Directory name="jscripts" />

                <Directory name="langs" />

              </Directory>

            </Directory>

            <Directory name="utils" />

          </Directory>

        </Directory>

      </Directory>

    </Directory>

  </Directory>

</Directory>


I'm expecting to find something such as

  <Directory name="abc" xmlns="">
    <Directory name="myapp">
      <Directory name="images"/>
    </Directory>
  </Directory>

but that doesn't exist in the XML.  Where is the XPATH finding that path?

This is the code I'm using, which does bring back a path:

XmlNode xn = paramXdoc.SelectSingleNode(myXPATH);

I've been using this tool to check the XPATH: http://www.zrinity.com/developers/xml/xpath/.  I can't tell if the tool is saying the XPATH does exists.  It does bring back results.
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

//Directory[@name='abc']//Directory[@name='myapp']//Directory[@name='images']

This XPath goes down the tree to find a Directory[@name='abc']
Then it goes back up to the root because of '//'
finds a Directory[@name='myapp']
Then it goes back up to the root because of '//'
and then it finds Directory[@name='images']

basically it returns all the Directory elements that have an attribute name equal to 'images'
and tests if the other two conditions are met

You can't use XPath to construct XML, you can only use it to select nodes

If you want to find this in your XML
  <Directory name="abc" xmlns="">
    <Directory name="myapp">
      <Directory name="images"/>
    </Directory>
  </Directory>

you need
1. that node existing in the original XML
2. this XPath: //Directory[@name='abc'][Directory[@name='myapp'][Directory[@name='images']]]

cheers

Geert
Avatar of brettr
brettr

ASKER

Thanks.  I go through the string and replace all "//" with "[".  I want to replace the first [Directory with //Directory.  I'm trying to create a regex using this:

^\[   //doesn't work

and variations of it give an unterminated string.  Do you have any suggestions?

Basically, I'm trying to change
//Directory[@name='abc']//Directory[@name='myapp']//Directory[@name='images']

into

//Directory[@name='abc'][Directory[@name='myapp'][Directory[@name='images']]]
make "\]$" become "]]]"
and make "\]//" become"]["
the"^" asks for the start of the string and that is exactly the one you want to keep intact

cheers

Geert
Avatar of brettr

ASKER

Sorry, the string actually looks like this after some initial replacements:

[Directory[@name='abc'][Directory[@name='myapp']]

That's why I need the carrot.  Replace the first occurance of "[" with "//".  But it keeps throwing the unterminated string error.

To get the last few brackets correct, I'll initially count all of the "//" and subtract one.  Then append that many brackets.
Avatar of brettr

ASKER

I have it working now.  The carrot needed to be the first character.

Just so I'm clear on how the XPATH should look, if I want a node structure such as this:

root
    sub1
         sub1.1
               sub1.11

I'll need an XPATH that looks like this:

//Directory[@name='root'][Directory[@name='sub1'][Directory[@name='sub1.1'][Directory[@name='sub1.11']]

or does it require most nesting?
add wo ]] at the end, and you would be OK
Avatar of brettr

ASKER

I've been trying but get this and crash.  I don't see any invalid token in this XPATH:

//Directory[@name='abc'][Directory[@name='aspnet_client'][Directory[@name='system_web']]

I'm looking in this XML:

<root xmlns="root">
  <Directory name="abc" xmlns="">
    <Directory name="account" />
    <Directory name="App_Data" />
    <Directory name="aspnet_client" />
  </Directory>
</root>

system_web is going to be a child of aspnet_client.  Since system_web doesn't exist, I should just get back a null.  Instead, I get this:

An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.dll

Additional information: '//Directory[@name='abc'][Directory[@name='aspnet_client'][Directory[@name='system_web']]' has an invalid token.

I'm using the SelectSingleNode() function with the above XPATH.  Do you see any problems with the XPATH syntax?
Avatar of brettr

ASKER

I see the problem now.  The opening bracket that is before
Directory[@name='system_web']

should it be there?  That would mean I need three closing brackets instead of two.
yes ]]] to end
Avatar of brettr

ASKER

Each sub folder needs to be nested that way?  If I have four subfolders, then I'll need four ending brackets and so on?
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial