XSLT:如何组合节点名称和字符串以用作匹配元素的选择器

时间:2013-01-06 20:00:13

标签: xml xslt xpath

我正在尝试使用祖父节点并将字符串与变量合并以匹配另一个节点,但是以下语句失败:

<xsl:apply-templates select="/data/*[name() = $grandparent]/entry[@id = current()/item/@id]" mode="slides"/>

这是变量:

<xsl:variable name="grandparent" select="concat(name(../..),'-','slides')">

完整XML:

<?xml version="1.0" encoding="utf-8" ?>
<data>
  <navigation>
    <page handle="work" id="2">
        <name>Work</name>
    </page>
    <page handle="studio" id="3">
        <name>Our Studio</name>
        <page handle="people" id="8">
            <name>People</name>
        </page>
        <page handle="process" id="7">
            <name>Process</name>
        </page>
    </page>
    <page handle="journal" id="4">
        <name>Journal</name>
    </page>
    <page handle="media" id="5">
        <name>Media</name>
        <page handle="press" id="25">
            <name>Press</name>
        </page>
        <page handle="publication" id="26">
            <name>Publication</name>
        </page>
    </page>
    <page handle="contact" id="6">
        <name>Contact</name>
    </page>
</navigation>

<work-slides>
    <section id="3" handle="work-slides">Work Slides</section>
    <entry id="105">
        <title handle="auto-4">Auto 4</title>
        <project>
            <item id="101" handle="autographer" section-handle="work" section-name="Work">Autographer</item>
        </project>
        <image size="272 KB" path="/uploads/slides" type="image/jpeg">
            <filename>auto4.jpg</filename>
        </image>
    </entry>
    <entry id="104">
        <title handle="auto-3">Auto 3</title>
        <project>
            <item id="101" handle="autographer" section-handle="work" section-name="Work">Autographer</item>
        </project>
        <image size="194 KB" path="/uploads/slides" type="image/jpeg">
            <filename>auto3.jpg</filename>
        </image>
    </entry>
</work-slides>
<work>
    <section id="1" handle="work">Work</section>
    <category link-id="11" link-handle="technology" value="Technology">
        <entry id="101">
            <name handle="autographer">Autographer</name>

            <category>
                <item id="11" handle="technology" section-handle="category" section-name="Category">Technology</item>
            </category>

            <slides>

                <item id="105" handle="auto4jpg" section-handle="work-slides" section-name="Work Slides">auto4.jpg</item>
            </slides>
        </entry>
        <entry id="39">
            <name handle="urraco">URRACO</name>

            <category>
                <item id="11" handle="technology" section-handle="category" section-name="Category">Technology</item>
            </category>

            <slides>
                <item id="104" handle="auto3jpg" section-handle="work-slides" section-name="Work Slides">auto3</item>
            </slides>
        </entry>
    </category>
</work>

以下是Full XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl"
>

<xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes" />

<!-- outline -->
<xsl:template match="/">
   <response>
        <xsl:for-each select="data/navigation/page">
            <xsl:element name="{@handle}">
                <xsl:attribute name="id"><xsl:value-of select="current()/@id"/></xsl:attribute>
                <xsl:value-of select="name"/>
                <xsl:apply-templates select="/data/*[name()=current()/@handle]" mode="page"/>
            </xsl:element>
        </xsl:for-each>
    </response>
</xsl:template>

<xsl:template match="/data/work" mode="page">
    <xsl:for-each select="category">
        <xsl:element name="{@link-handle}">
            <xsl:attribute name="id"><xsl:value-of select="@link-id"/></xsl:attribute>
            <xsl:value-of select="@value"/>
            <xsl:apply-templates select="entry" mode="category"/>
        </xsl:element>
    </xsl:for-each>
</xsl:template>

<xsl:template match="entry" mode="category">
    <xsl:variable name="grandparent" select="concat(name(../..),'-','slides')">
    <article>
        <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
        <xsl:for-each select="child::*">
            <xsl:element name="{local-name()}">
                <xsl:choose>
                    <xsl:when test="local-name() = 'slides'">
                        <xsl:apply-templates select="/data/*[name() = $grandparent]/entry[@id = current()/item/@id]" mode="slides"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:element>
        </xsl:for-each>
    </article>
</xsl:template>


<xsl:template match="entry" mode="slides">
    <slide>
        <xsl:for-each select="child::*">
            <xsl:element name="{local-name()}">
                <xsl:choose>
                    <xsl:when test="local-name() = 'image'">
                        <xsl:value-of select="concat($workspace, @path, '/', filename)"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:element>
        </xsl:for-each>
    </slide>
</xsl:template>

</xsl:stylesheet>

有可能做到以上几点吗?我不明白为什么它应该失败。

1 个答案:

答案 0 :(得分:4)

<xsl:variable name="grandparent" select="concat(name(../..),'-','slides')">

这在语法上是非法的。具有未闭合起始标记的XML元素。

必须

<xsl:variable name="grandparent" select="concat(name(../..),'-','slides')"/>