In cases where your XML contains namespaces, like this example:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> <sheets> <sheet name="Sheet1" sheetId="1" r:id="rId1"/> </sheets> </workbook>
you may encounter issues when using XPath queries like "/workbook/sheets/sheet[1]". This is because the elements are bound to a namespace, and your XPath is looking for elements in the default, anonymous namespace.
There are multiple ways to handle namespaces in XPath queries:
The recommended approach is to register the namespace with a prefix. This simplifies XPath development and maintenance:
NamespaceContext namespaceContext = new NamespaceContext() { @Override public String getNamespaceURI(String prefix) { if ("xssf".equals(prefix)) { return "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; } return null; } @Override public String getPrefix(String namespaceURI) { return null; } @Override public Iterator getPrefixes(String namespaceURI) { return null; } }; XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(namespaceContext);
With this setup, you can use simplified XPath queries:
/xssf:workbook/xssf:sheets/xssf:sheet[1]
If you prefer not to use namespace prefixes, you can craft XPath expressions that use a generic match with a predicate filter:
/*[local-name()='workbook' and namespace-uri()='http://schemas.openxmlformats.org/spreadsheetml/2006/main'] /*[local-name()='sheets' and namespace-uri()='http://schemas.openxmlformats.org/spreadsheetml/2006/main'] /*[local-name()='sheet' and namespace-uri()='http://schemas.openxmlformats.org/spreadsheetml/2006/main'][1]
This method results in verbose XPath expressions that may be difficult to read and maintain.
You can also ignore namespaces by matching only on the local name:
/*[local-name()='workbook']/*[local-name()='sheets']/*[local-name()='sheet'][1]
However, this approach risks selecting the wrong elements in cases of mixed vocabularies that use the same local names across different namespaces.
The above is the detailed content of How do you handle namespaces in XPath queries when your XML contains them?. For more information, please follow other related articles on the PHP Chinese website!