Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, July 22, 2016 7:57 AM
Hi,
I met a question that I can't get the attribute in the xml when the colon exists. For example, I want to get the value but it seems not working with my code. Any suggestions?
for eg:
<ext-link xlink:href="www.effectivehealthcare.ahrq.gov/" ext-link-type="uri">www.effectivehealthcare.ahrq.gov/</ext-link>
my code :
var term = from x in xDocFile.XPathSelectElements("//ext-link[xlink:href=text()]") select x;
All replies (6)
Monday, July 25, 2016 2:02 PM âś…Answered
The problem is that your example is talking about 2 different situations. In the first situation you have an attribute and you want to verify it matches the element's text. The second situation however is when the attribute doesn't exist at all. This is the case because xlink:href and xlink are 2 entirely different attributes. You could have put a:href and b as attribute names and it would be the same situation. Note that I maintain that 3 is actually an invalid XML state. You have an attribute that matches the name of a namespace prefix. This is bad XML. Personally I think the XML should be rejected and, hence, XSD is the correct solution to reject this file altogether.
Nevertheless here's the XPath that should work.
var badElements = doc.XPathSelectElements("//ext-link[@xlink:href!=text() or not(@xlink:href)]", doc.Root.CreateNavigator());
Friday, July 22, 2016 8:14 AM
The bit before the colon (xlink) is a namespace. You can find out about them here (or just search for "XML namespaces").
In your case, you could use the overload of XPathSelectElements that allows you to resolve the namespace.
Friday, July 22, 2016 9:18 AM
i got solution
var varSearchTerm12 = from x in xDocFile.XPathSelectElements("//ext-link[namespace::*[name() = 'xlink']]") select x;
but i have one query that is how to check attribute text with tag text ,got only unmatch record
Here, xlink:href is compulsory in ext-link
for eg.:
Input :
<ext-link xlink:href="www.effectivehealthcare.ahrq.gov/" ext-link-type="uri">www.effectivehealthcare.ahrq.gov/</ext-link>
<ext-link xlink:href="www.effectivehealthcare.ahrq.gov/" ext-link-type="uri">www.test.com/</ext-link>
<ext-link xlink="www.effectivehealthcare.ahrq.gov/" ext-link-type="uri">www.effectivehealthcare.ahrq.gov/</ext-link>
output:
<ext-link xlink:href="www.effectivehealthcare.ahrq.gov/" ext-link-type="uri">www.test.com/</ext-link>
<ext-link xlink="www.effectivehealthcare.ahrq.gov/" ext-link-type="uri">www.effectivehealthcare.ahrq.gov/</ext-link>
Friday, July 22, 2016 1:50 PM
That type of check is best handled by using an XSD. XPath is for finding elements, not validating them.
If xlink:href is required then mark it as required in the XSD. Then when loading the document validate it against the XSD to ensure it is valid.
If you really wanted to use XPath then use the not() function to get all elements that are missing the given attribute. Something like this:
XPathSelectElement("/someElement[not(@attrib)]"
Michael Taylor
http://www.michaeltaylorp3.net
Monday, July 25, 2016 4:32 AM
but i have need to check attribute value with tag text
means that
check "xlink:hre" value with" ext-link" text
for brief please check above description
Monday, July 25, 2016 5:05 PM
Please check this answer which describes how to set up your xpath query to use a namespace via an XmlNamespaceManager.