Comments (4)
using System;
using AngleSharp.Dom;
using AngleSharp.XPath;
using AngleSharp.Xml.Parser;
public class Program
{
public static string Xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"><url><loc>https://www.test.com/de/accounts/profile</loc><xhtml:link rel=\"alternate\" hreflang=\"fr\" href=\"https://www.test.com/fr/accounts/profile\" /><xhtml:link rel=\"alternate\" hreflang=\"en\" href=\"https://www.test.com/en/accounts/profile\" /><xhtml:link rel=\"alternate\" hreflang=\"it\" href=\"https://www.test.com/it/accounts/profile\" /><changefreq>weekly</changefreq><priority>0.4</priority></url></urlset>";
public static void Main()
{
var parser = new XmlParser();
var doc = parser.ParseDocument(Xml);
var nodeList = doc.DocumentElement.SelectNodes("//urlset/url/xhtml:link");
foreach (var entry in nodeList)
Console.WriteLine(string.Join(",", entry.TextContent));
}
}
from anglesharp.xpath.
Good day!
I'll see it later.
from anglesharp.xpath.
I believe the best way to fix is adding SelectNodesNS and SelectSingleNodeNS
from anglesharp.xpath.
Problem solved in AngleSharp.XPath 1.1.6
[Test]
public void SelectSingleNode_IgnoreNamespaces_ShouldReturnNode()
{
// Arrange
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"><url><loc>https://www.test.com/de/accounts/profile</loc><xhtml:link rel=\"alternate\" hreflang=\"fr\" href=\"https://www.test.com/fr/accounts/profile\" /><xhtml:link rel=\"alternate\" hreflang=\"en\" href=\"https://www.test.com/en/accounts/profile\" /><xhtml:link rel=\"alternate\" hreflang=\"it\" href=\"https://www.test.com/it/accounts/profile\" /><changefreq>weekly</changefreq><priority>0.4</priority></url></urlset>";
var parser = new XmlParser();
var doc = parser.ParseDocument(xml);
// Act
var node = doc.DocumentElement.SelectSingleNode("/urlset/url/link");
// Assert
Assert.IsNotNull(node);
Assert.That(node.NodeName, Is.EqualTo("xhtml:link"));
}
[Test]
public void SelectSingleNode_DontIgnoreNamespaces_ShouldReturnNode()
{
// Arrange
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"><url><loc>https://www.test.com/de/accounts/profile</loc><xhtml:link rel=\"alternate\" hreflang=\"fr\" href=\"https://www.test.com/fr/accounts/profile\" /><xhtml:link rel=\"alternate\" hreflang=\"en\" href=\"https://www.test.com/en/accounts/profile\" /><xhtml:link rel=\"alternate\" hreflang=\"it\" href=\"https://www.test.com/it/accounts/profile\" /><changefreq>weekly</changefreq><priority>0.4</priority></url></urlset>";
var parser = new XmlParser();
var doc = parser.ParseDocument(xml);
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
namespaceManager.AddNamespace("d", "http://www.sitemaps.org/schemas/sitemap/0.9");
// Act
var node = doc.DocumentElement.SelectSingleNode("/d:urlset/d:url/xhtml:link", namespaceManager, false);
// Assert
Assert.IsNotNull(node);
Assert.That(node.NodeName, Is.EqualTo("xhtml:link"));
}
from anglesharp.xpath.
Related Issues (18)
- get attribute value HOT 2
- Is the latest release 1.1.7 https://www.nuget.org/packages/AngleSharp.XPath/1.1.7 from NuGet not tagged in this GitHub repository? HOT 3
- Change Name getter in HtmlDocumentNavigator to return lower-case variant of name for (X)HTML element nodes HOT 3
- Wrong code in Prefix getter HOT 2
- MoveToParent doesn't take attribute navigation into account HOT 3
- Will 2.0 only support .NET (Core) and not .NET framework? HOT 1
- v2.0.0 regression with XPaths ending on `/@attr` HOT 3
- BUG: SelectSingleNode with search by attribute predicate only works if searched attribute is the first HOT 2
- Change default branch HOT 2
- Interested in placing this repo in the AngleSharp organization? HOT 3
- Regression: `2.0` now returns `Attr` nodes instead of their parents on xpath ending with `/@attr` HOT 4
- Problem with namespaces HOT 5
- HtmlDocumentNavigator is not correctly implemented.
- Xpath query support for Nodes? HOT 5
- Tag <br/> is ignored HOT 2
- How to locate child node based on node? HOT 1
- SelectSingleNode() doesn't select <tr>
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anglesharp.xpath.