Git Product home page Git Product logo

Comments (4)

denis-ivanov avatar denis-ivanov commented on September 27, 2024 1
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.

denis-ivanov avatar denis-ivanov commented on September 27, 2024

Good day!
I'll see it later.

from anglesharp.xpath.

yinyue200 avatar yinyue200 commented on September 27, 2024

I believe the best way to fix is adding SelectNodesNS and SelectSingleNodeNS

from anglesharp.xpath.

denis-ivanov avatar denis-ivanov commented on September 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.