How to deal with the presence or absence of xml namespaces using xslt

I have XML / TEI documents and I am writing XSLT 2.0 to extract their contents.

Almost all TEI documents do not have a namespace, but one has a default namespace ( xmlns="http://www.tei-c.org/ns/1.0"). Thus, all documents have the same aspect with fuzzy tags like <TEI>or <teiHeader>, but if I try to extract the contents, everything works with "non-namespaced-documents", but nothing (of course) is extracted from the name-document space.

So, I used the attribute xpath-default-namespace="http://www.tei-c.org/ns/1.0"and now (of course) the only document that works with names.

I cannot edit documents at all, so I ask if there is a way to dynamically change the xpath-default namespace to make xpaths work //teiHeaderas with both files with and without names

+3
source share
3 answers

If you are using XSLT 2.0 , you have the option for wildcard matching for the namespace in the node test.

eg. //*:teiHeader

http://www.w3.org/TR/xpath20/#node-tests

A node can also take the form *: NCName. In this case, the node test is true for any node of the main node, the axis view of the step, the local name corresponds to this NCName, regardless of its namespace or absence of a namespace.

Dimitre Novatchev, / .

XSLT/XPATH 2.0.

+1

, . . , , , .

( XSLT 1) DocBook XSLT. html/docbook.xsl common/stripns.xsl

, , ( , ), .

+1

, , :

//*[name()='teiHeader']

If you use this style for all location steps in any XPath expression, XPath expressions will only select elements by name , regardless of whether these elements belong to any namespace.

+1
source

Source: https://habr.com/ru/post/1741440/


All Articles