In XSLT, how do I get the filepath of the xml file of a certain element if that xml file was included with xinclude?

StackOverflow https://stackoverflow.com/questions/10620404

  •  09-06-2021
  •  | 
  •  

Question

I have these XML files:

master.xml (which uses XInclude to include child1.xml and child2.xml)
child1.xml
child2.xml

Both child1.xml and child2.xml contain a <section> element with some text.

In the XSLT transformation, I 'd want to add the name of the file the <section> element came from, so I get something like:

<section srcFile="child1.xml">Text from child 1.</section>
<section srcFile="child2.xml">Text from child 2.</section>

How do I retrieve the values child1.xml and child2.xml?

Was it helpful?

Solution

Unless you turn off that feature, all XInclude processors should add an @xml:base attribute with the URL of the included file. So you don't have to do anything, it should already be:

<section xml:base="child1.xml">Text from child 1.</section>
<section xml:base="child2.xml">Text from child 2.</section>

( If you want, you can use XSLT to transform the @xml:base attr into @srcFile. )

OTHER TIPS

I'm 99% sure that once xi:include has been processed, you have a single document (and single infoset) that won't let you determine which URL any given part of the document came from.

I think you will need to place that information directly in the individual included files. Having said that, you can still give document-uri a try, but I think all nodes will return the same URI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top