xml - 带参数的递归 XSL 模板调用

标签 xml xslt xpath recursion xslt-1.0

我正在尝试使用以下代码完成页面导航。 尽管这段代码被简化为重要部分,但我的想法应该很清楚。 ID 可以是任何给定的名称,并且可以以任何给定的顺序以任何给定的数字出现。

输入:定义排序集合中的上一页和下一页的页面:

<pages>
  <page id="page1" next="page2" />
  <page id="page2" prev="page1" next="page3" />
  <page id="page3" prev="page2" />
  <page id="test2" prev="test1" />
  <page id="test1" next="test2" />
  <page id="alone" />
</pages>

所需的输出(我无法生成):

<page id="page1">
  <link href="page1" class="active" />
  <link href="page2" />
  <link href="page3" />
</page>
<page id="page2">
  <link href="page1" />
  <link href="page2" class="active" />
  <link href="page3" />
</page>
<page id="page3">
  <link href="page1" />
  <link href="page2" />
  <link href="page3" class="active" />
</page>
<page id="test1">
  <link href="test1" class="active" />
  <link href="test2 />
</page>
<page id="test2">
  <link href="test1" />
  <link href="test2" class="active" />
</page>
<page id="alone">
</page>

到目前为止我对 XSL 所做的尝试:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:for-each select="pages/page">
  <page id="{@id}">

    <xsl:call-template name="prev-page">
      <xsl:with-param name="id">
        <xsl:value-of select="@prev" />
      </xsl:with-param>
    </xsl:call-template>

    <link href="{@id}" class="active" />

    <xsl:call-template name="next-page">
      <xsl:with-param name="id">
        <xsl:value-of select="@next" />
      </xsl:with-param>
    </xsl:call-template>

  </page>
</xsl:for-each>

<xsl:template name="prev-page">
  <xsl:param name="id" />

  <xsl:if test="string-length($id) > 0">
    <link href="{$id}" />
    <xsl:call-template name="prev-page">
      <xsl:with-param name="id">
        <!-- I believe in here lies the problem -->
        <xsl:value-of select="(/pages/page[@id = $id])[0]/@prev" />
      </xsl:with-param>
    </xsl:call-template>
  </xsl:if>
</xsl:template>
... (same for page-next)
</xsl:stylesheet>

最佳答案

让我建议一种完全不同的方法。首先,让我们按所需的顺序对页面进行排序。完成后,依次输出每个页面以及(所有)其他页面的链接:

XSLT 1.0

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

<!-- first-pass -->
<xsl:key name="page" match="page" use="@id" />

<xsl:variable name="sorted-pages">
    <xsl:apply-templates select="pages/page[not(@prev)]" mode="first-pass"/>
</xsl:variable>

<xsl:template match="page" mode="first-pass">
    <xsl:copy-of select="."/>
    <xsl:apply-templates select="key('page', @next)" mode="first-pass"/>
</xsl:template>

<xsl:variable name="sorted-pages-set" select="exsl:node-set($sorted-pages)/page" />

<!-- final-output -->
<xsl:template match="/">
    <pages>
        <xsl:for-each select="$sorted-pages-set">
        <xsl:variable name="current-id" select="@id" />
            <page id="{$current-id}">
                <xsl:apply-templates select="$sorted-pages-set">
                    <xsl:with-param name="current-id" select="$current-id"/>
                </xsl:apply-templates>
            </page>
        </xsl:for-each>
    </pages>
</xsl:template>

<xsl:template match="page">
    <xsl:param name="current-id" />
    <link href="{@id}">
        <xsl:if test="@id=$current-id">
            <xsl:attribute name="class">active</xsl:attribute>
        </xsl:if>
    </link>
</xsl:template>

</xsl:stylesheet>

应用于以下测试输入:

<pages>
    <page id="alone" />
    <page id="fourth" prev="third" next="MISSING" />
    <page id="second" prev="first" next="third" />
    <page id="ii" prev="i" next="iii" />/>  
    <page id="first" next="second" />
    <page id="third" prev="second" next="fourth" />
    <page id="solo" />
    <page id="i" next="ii" />
    <page id="b" prev="a" /> 
    <page id="iii" prev="ii" />
    <page id="PROBLEM" prev="MISSING"/>
    <page id="a" next="b" />
</pages>

产生以下结果:

<?xml version="1.0" encoding="UTF-8"?>
<pages>
   <page id="alone">
      <link href="alone" class="active"/>
      <link href="first"/>
      <link href="second"/>
      <link href="third"/>
      <link href="fourth"/>
      <link href="solo"/>
      <link href="i"/>
      <link href="ii"/>
      <link href="iii"/>
      <link href="a"/>
      <link href="b"/>
   </page>
   <page id="first">
      <link href="alone"/>
      <link href="first" class="active"/>
      <link href="second"/>
      <link href="third"/>
      <link href="fourth"/>
      <link href="solo"/>
      <link href="i"/>
      <link href="ii"/>
      <link href="iii"/>
      <link href="a"/>
      <link href="b"/>
   </page>
   <page id="second">
      <link href="alone"/>
      <link href="first"/>
      <link href="second" class="active"/>
      <link href="third"/>
      <link href="fourth"/>
      <link href="solo"/>
      <link href="i"/>
      <link href="ii"/>
      <link href="iii"/>
      <link href="a"/>
      <link href="b"/>
   </page>
   <page id="third">
      <link href="alone"/>
      <link href="first"/>
      <link href="second"/>
      <link href="third" class="active"/>
      <link href="fourth"/>
      <link href="solo"/>
      <link href="i"/>
      <link href="ii"/>
      <link href="iii"/>
      <link href="a"/>
      <link href="b"/>
   </page>
   <page id="fourth">
      <link href="alone"/>
      <link href="first"/>
      <link href="second"/>
      <link href="third"/>
      <link href="fourth" class="active"/>
      <link href="solo"/>
      <link href="i"/>
      <link href="ii"/>
      <link href="iii"/>
      <link href="a"/>
      <link href="b"/>
   </page>
   <page id="solo">
      <link href="alone"/>
      <link href="first"/>
      <link href="second"/>
      <link href="third"/>
      <link href="fourth"/>
      <link href="solo" class="active"/>
      <link href="i"/>
      <link href="ii"/>
      <link href="iii"/>
      <link href="a"/>
      <link href="b"/>
   </page>
   <page id="i">
      <link href="alone"/>
      <link href="first"/>
      <link href="second"/>
      <link href="third"/>
      <link href="fourth"/>
      <link href="solo"/>
      <link href="i" class="active"/>
      <link href="ii"/>
      <link href="iii"/>
      <link href="a"/>
      <link href="b"/>
   </page>
   <page id="ii">
      <link href="alone"/>
      <link href="first"/>
      <link href="second"/>
      <link href="third"/>
      <link href="fourth"/>
      <link href="solo"/>
      <link href="i"/>
      <link href="ii" class="active"/>
      <link href="iii"/>
      <link href="a"/>
      <link href="b"/>
   </page>
   <page id="iii">
      <link href="alone"/>
      <link href="first"/>
      <link href="second"/>
      <link href="third"/>
      <link href="fourth"/>
      <link href="solo"/>
      <link href="i"/>
      <link href="ii"/>
      <link href="iii" class="active"/>
      <link href="a"/>
      <link href="b"/>
   </page>
   <page id="a">
      <link href="alone"/>
      <link href="first"/>
      <link href="second"/>
      <link href="third"/>
      <link href="fourth"/>
      <link href="solo"/>
      <link href="i"/>
      <link href="ii"/>
      <link href="iii"/>
      <link href="a" class="active"/>
      <link href="b"/>
   </page>
   <page id="b">
      <link href="alone"/>
      <link href="first"/>
      <link href="second"/>
      <link href="third"/>
      <link href="fourth"/>
      <link href="solo"/>
      <link href="i"/>
      <link href="ii"/>
      <link href="iii"/>
      <link href="a"/>
      <link href="b" class="active"/>
   </page>
</pages>

关于xml - 带参数的递归 XSL 模板调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27569275/

相关文章:

c# - HtmlAgilityPack NextSibling.InnerText 值为空

xpath - 如何仅在Xpath中提取数据匹配字符串

java - 空标签的 XML 验证

javascript,xml,查询文件以查看是否 tag==1 然后执行某些操作

java - 使用 XSLT 转换在 XML 中创建 xmlns 属性

xml - 使用 XSLT 将 XML 标签转换为任意位置的属性

python - 跳出 while True 循环

php - XML 到 MySQL 检查 XML 字段是否为空

XSLT 复制/复制而不保留命名空间

xslt - Xpath表达式VS XSL变量