XSLT用于具有相同名称的多个元素

时间:2018-09-11 03:05:23

标签: xml unix xslt xpath xslt-2.0

基本上,我在编写xsl时需要传递目录名,输出应返回该文件的所有文件名。请参见下面的xml结构。

    <folder>
    <directory name='a'>
    <directory name='b/c'>
    <file name ='x'>
    </file>
    </directory>
    <directory name ='v'>
    <file name='f'>
    </file>
    </directory>
    </directory>
    <directory name ='z'>
    <file name= 'c'>
        </file>
        </directory>
</folder>

例如,如果目录名称= b / c,则应返回文件名,并附加其目录名称b / c / x(output)。它应该以通用方式在基于目录名称的位置上返回关联的文件名并附加其目录名称。请帮我如何编写xsl

2 个答案:

答案 0 :(得分:2)

我用祖先::目录的xsl:value-of提出了这个解决方案。

<xsl:template match="file">
    <p>
        <xsl:value-of select="ancestor::directory/@name" separator="/"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="@name"/>
    </p>

</xsl:template>

请参见上面的XML结构的结果:http://xsltransform.net/ei5PwjK

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="text" indent="yes"/>
    <xsl:template match="/folder">
        <xsl:for-each select="directory[@name='b/c']/file">
            <xsl:value-of select="../@name"/>/<xsl:value-of select="@name"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
相关问题