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='mya pp']//Dire ctory[@nam e='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.
//Directory[@name='abc']//
--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-
<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
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.
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='mya pp']//Dire ctory[@nam e='images' ]
into
//Directory[@name='abc'][D irectory[@ name='myap p'][Direct ory[@name= 'images']] ]
^\[ //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']//
into
//Directory[@name='abc'][D
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
and make "\]//" become"]["
the"^" asks for the start of the string and that is exactly the one you want to keep intact
cheers
Geert
ASKER
Sorry, the string actually looks like this after some initial replacements:
[Directory[@name='abc'][Di rectory[@n ame='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.
[Directory[@name='abc'][Di
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.
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='sub 1'][Direct ory[@name= 'sub1.1'][ Directory[ @name='sub 1.11']]
or does it require most nesting?
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'][
or does it require most nesting?
add wo ]] at the end, and you would be OK
ASKER
I've been trying but get this and crash. I don't see any invalid token in this XPATH:
//Directory[@name='abc'][D irectory[@ name='aspn et_client' ][Director y[@name='s ystem_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.XPathExc eption' occurred in System.Xml.dll
Additional information: '//Directory[@name='abc'][ Directory[ @name='asp net_client '][Directo ry[@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?
//Directory[@name='abc'][D
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.XPathExc
Additional information: '//Directory[@name='abc'][
I'm using the SelectSingleNode() function with the above XPATH. Do you see any problems with the XPATH syntax?
ASKER
I see the problem now. The opening bracket that is before
Directory[@name='system_we b']
should it be there? That would mean I need three closing brackets instead of two.
Directory[@name='system_we
should it be there? That would mean I need three closing brackets instead of two.
yes ]]] to end
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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'][D
cheers
Geert