1

我有如下所示的 XML:

<library>
    <album>
        <!-- Album title, length, # of tracks, etc -->
        <playas>
            <extension>.mp3</extension>
            <type>audio/mpeg</type>
        </playas>
        <playas>
            <extension>.ogg</extension>
            <type>audio/ogg</type>
        </playas>

        <track>
            <!-- Track number, title, length -->
        </track>
        <!-- Many, many more tracks -->
    </album>
</library>

在我的 XLS 文件中,我想用它xls:for-each来确定每个轨道的sourcesrc 属性。这是不起作用的:

<xsl:for-each select="track">
    <audio>
        <!-- Parent (album)'s playas elements -->
        <xsl:for-each select="../playas">
            <source>
                <xsl:attribute name="src">
                    <!-- Parent (album)'s title + '-src', where I store the audio files
                         along with current node (playas)'s extension -->
                    <xsl:value-of select="../title" />-src/<xsl:value-of select="title" /><xsl:value-of select="extension" />
                </xsl:attribute>
                <xsl:attribute name="type">
                    <xsl:value-of select="srctype" />
                </xsl:attribute>
            </source>
        </xsl:for-each>
    </audio>
</xsl:for-each>

上面的代码给出了<source src="AlbumTitle-src/.mp3" type="audio/mpeg"> (or .ogg and audio/ogg)- 当前track的标题不存在。但是,为每个曲目正确创建了代码,所以我只需要知道如何获取当前曲目的标题(来自 playas 的上下文)。

我也试过for-each select="preceding-sibling::playas"了,没有运气。如何在playas不“离开” current的情况下访问每个孩子的孩子track

编辑:我知道这很容易通过简单地<source>为每个 playas 扩展硬编码 a 来完成,但我想用最少的实现来做到这一点。

编辑 2: @Peter:我对每首曲目的预期输出(在等效的 HTML 中)是

<audio>
    <source src="AlbumTitle-src/track.mp3" type="audio/mpeg" />
    <source src="AlbumTitle-src/track.ogg" type="audio/ogg" />
</audio>
4

1 回答 1

1

当你在里面时,<xsl:for-each select="../playas">你已经在playas元素上下文中了。所以要检索它的title孩子,你只需要<xsl:value-of select="title" />. 并且要检索轨道标题,您可以在trackfor-each 内部设置一个变量并在源输出元素中使用它,例如:

<xsl:for-each select="track">
<audio>
    <xsl:variable name="track_title" select="title" />
    <!-- Parent (album)'s playas elements -->
    <xsl:for-each select="../playas">
        <source>
            <xsl:attribute name="src">
                <!-- Parent (album)'s title + '-src', where I store the audio files
                     along with current node (playas)'s extension -->
                <xsl:value-of select="title" />-src/<xsl:value-of select="$track_title" /><xsl:value-of select="extension" />
            </xsl:attribute>
            <xsl:attribute name="type">
                <xsl:value-of select="srctype" />
            </xsl:attribute>
        </source>
    </xsl:for-each>
</audio>

于 2013-07-04T05:46:51.670 回答