草庐IT

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

coder 2024-07-05 原文

我正在尝试使用以下代码完成页面导航。 虽然这段代码被简化到重要部分,但我的想法应该很清楚。 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/

有关xml - 带参数的递归 XSL 模板调用的更多相关文章

  1. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  2. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  3. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  4. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  5. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  6. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

  7. 【鸿蒙应用开发系列】- 获取系统设备信息以及版本API兼容调用方式 - 2

    在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList​()Obt

  8. ruby-on-rails - Mandrill API 模板 - 2

    我正在使用Mandrill的RubyAPIGem并使用以下简单的测试模板:testastic按照Heroku指南中的示例,我有以下Ruby代码:require'mandrill'm=Mandrill::API.newrendered=m.templates.render'test-template',[{:header=>'someheadertext',:main_section=>'Themaincontentblock',:footer=>'asdf'}]mail(:to=>"JaysonLane",:subject=>"TestEmail")do|format|format.h

  9. C51单片机——实现用独立按键控制LED亮灭(调用函数篇) - 2

    说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时

  10. ruby - 如何找到调用当前方法的方法 - 2

    如何找到调用此方法的位置?defto_xml(options={})binding.pryoptions=options.to_hifoptions&&options.respond_to?(:to_h)serializable_hash(options).to_xml(options)end 最佳答案 键入caller。这将返回当前调用堆栈。文档:Kernel#caller.例子[0]%rspecspec10/16|===================================================62=====

随机推荐