I have a filesystem like XML structure, and now I want to get the “filepath” of a . I tried the following XSLT, but it doesn’t work. I only get these errors:
Warning:
XSLTProcessor::transformToXml():
Templates: in
C:\Users\Ludger\Documents\XAMPP\htdocs\CloudAmp\devel\php\localAudioFileLocationScanner.php
on line 60Warning:
XSLTProcessor::transformToXml(): #0
name //file in
C:\Users\Ludger\Documents\XAMPP\htdocs\CloudAmp\devel\php\localAudioFileLocationScanner.php
on line 60Warning:
XSLTProcessor::transformToXml(): #1
name //file in
C:\Users\Ludger\Documents\XAMPP\htdocs\CloudAmp\devel\php\localAudioFileLocationScanner.php on line 60[…]
Warning: XSLTProcessor::transformToXml():
xsltApplyXSLTTemplate: A potential
infinite template recursion was
detected. You can adjust xsltMaxDepth
(–maxdepth) in order to raise the
maximum number of nested template
calls and variables/params (currently
set to 3000). in
C:\Users\Ludger\DocumentsXA
MPP\htdocs\CloudAmp\devel\php\localAudioFileLocationScanner.php
on line 60
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/dir">
<xsl:apply-templates select="@name" />
<xsl:apply-templates select="parent::dir" />
</xsl:template>
<xsl:template match="//file">
<xsl:apply-templates select="@name" />
<xsl:apply-templates select="parent::dir" />
</xsl:template>
</xsl:stylesheet>
Source XML:
<root>
<path val="C:/Users/">
<file name="a.txt"/>
<dir name="aaa">
<file name="b.txt"/>
<file name="c.txt"/>
</dir>
<dir name="bbb">
<dir name="ccc">
<file name="d.txt"/>
</dir>
</dir>
</path>
</root>
I can’t get it working. It would be great if you can help me out.
What’s happening is that you’re matching any
fileelement, then applying templates to its parentdir, which is matched by the default template for elements, which applies templates to all of its children, resulting in another match of the samefileand starting an infinite recursion.(Note that your template for
direlements never matches anything, because it’s looking only fordirelements that are a child of the root node, of which you have none.)The following stylesheet:
Produces the following output:
Edit: I think it’s generally better to push forward through the document when backtracking can be avoided. The following stylesheet produces the same output as above, but is more efficient and elegant: