simonkimi / xpath_selector

An XPath selector for locating Html and Xml elements
BSD 3-Clause "New" or "Revised" License
20 stars 4 forks source link

How to ignore namespaces? #2

Closed bubnenkoff closed 2 years ago

bubnenkoff commented 2 years ago

Hi! I am trying to parse XML. I need to ignore namespaces. How to do it?

simonkimi commented 2 years ago

Sorry, I didn't take into account the namespace aspect of XML. But it looks like it ignores namespace by default

simonkimi commented 2 years ago

I supported namespace selection in version 1.1.0. In this version, you can use/namespace:local-nameto select nodes with namespaces. Ignore the namespace with /*[local-name()='name'] Use /*[namespace()='namespace'] to select a namespace. However, this is not the final plan, please leave me your message, if you have any suggestions and comment.

bubnenkoff commented 2 years ago

Sorry, but I can't get it work. I need to extract purchaseNumber. I tried different approaches like:

 final result5 = htmlDom.queryXPath('//*[local-name()="purchaseNumber"]');

http://vpaste.net/BQtLS

simonkimi commented 2 years ago

Have you upgrade to 1.1.0 or 2.0.0? It works fine for me.

import 'dart:io';
import 'package:xpath_selector/xpath_selector.dart';

Future<void> main() async {
  final file = File('test.xml').readAsStringSync();
  final selector = XPath.xml(file).query('//*[local-name()="purchaseNumber"]');
  print(selector.elements);
}
[<ns9:purchaseNumber>0376200002921000071</ns9:purchaseNumber>, <ns9:purchaseNumber>0159</ns9:purchaseNumber>]
bubnenkoff commented 2 years ago

Big thanks! it's work!