0

Consider the following xml:

<folder>
    <name>folderA</name>
     <contents>
         <folder>
             <name>folderB</name>
             <contents>
                  <file>
                      <name>fileC</name>
                  <file>
             </contents>
         </folder>
     </contents>
</folder>

which represents the simple file structure:

folderA/
 L  folderB/
    L   fileC

In XSL, I would like to be able to generate the path of the file in a file template. Therefore it seems I would need to recursivly ascend the node tree to get the names of the folders this file is in.

How would you fill the ??? in next xsl template

<xsl:template match="file">
     <a href="{???}"><xsl:value-of name="name" /></a>
</xsl:template>

to finally get:

<a href="folderA/folderB/fileC">fileC</a>
romeovs
  • 5,785
  • 9
  • 43
  • 74

1 Answers1

0

Here ancestor:: is your friend. Try something like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

    <xsl:template match="file">
        <a>
            <xsl:attribute name="href">
                <xsl:apply-templates select="ancestor::folder" />
                <xsl:value-of select="name"/>
            </xsl:attribute>
            <xsl:value-of select="name"/>
        </a>
    </xsl:template>

    <xsl:template match="folder" >
        <xsl:value-of select="name"/>
        <xsl:text>/</xsl:text>
    </xsl:template>

    <xsl:template match="/" >
        <xsl:apply-templates select="//file" />
    </xsl:template>

</xsl:stylesheet>
hr_117
  • 9,589
  • 1
  • 18
  • 23