1

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

<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>

这是完整的 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>

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

4

1 回答 1

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

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

必须是

<xsl:variable name="grandparent" select="concat(name(../..),'-','slides')"/>
于 2013-01-06T20:20:31.780 回答