草庐IT

xml - XSLT - 递归模板

coder 2024-06-27 原文

我是 XSLT 的新手,我在模板递归方面遇到了麻烦。 我的问题是:我有两个文件 xml,第一个是:

    <EV>
        <forma codice="f01">
            ...
            <forma codice="f02">
                ...
                <forma>
                    ...
                </forma>
            </forma>
        </forma>
    </EV>

第二个是:

<forme> <!--codice_forma è "fxx"-->
<famiglia tipo="quadrilatero">
    <forma codice="f00" figura="quadrato"/>
    <forma codice="f01" figura="rettangolo"/>
</famiglia>
<famiglia tipo="triangolo">
    <forma codice="f02" figura="triangolo equilatero"/>
    <forma codice="f03" figura="triangolo rettangolo"/>
</famiglia>
</forme>

现在我必须得到这个转换结果,并加入属性“codice”:

<EV>
        <forma tipo="quadrilatero" figura="rettangolo">
           <forma ...>
               <forma ...>
                   ...
               </forma>
           </forma>
        </forma>
</EV>

我的 XSLT 文件不包含标签“forma”:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <evoluzione>
        <!-- Ciclo su tutti i possibili snapshot presenti -->
        <xsl:for-each select="/evoluzione/snapshot">
            <!-- Creo il tag snapshot -->
            <snapshot id="{@id}" label="{@label}">
                <!-- Creo il tag data -->
                <data giorno="{substring(@data,1,2)}" mese="{substring(@data,4,2)}" anno="{substring(@data,7,4)}"/>
                <!-- Ciclo su tutti i possibili esseri viventi presenti nello shapshot -->
                <xsl:for-each select="./EV">
                    <!-- Creo il tag EV -->
                    <EV cod_individuo="{@codice_individuo}" eta="{@eta}" salute="{@salute}" ciclo_di_vita="{@ciclo_di_vita}">
                        <!-- Invoco template sul file esseri_vienti.xml per ricavare l'aspettativa -->
                        <xsl:apply-templates select="document('/home/localhero/Scrivania/Progetto/esseri_viventi.xml')//EV">
                            <!-- Passo il parametro codInd al template -->
                            <xsl:with-param name="codInd" select="@codice_individuo"/>
                        </xsl:apply-templates>
                        <!-- Creo il tag specie -->
                        <specie>
                            <!-- Invoco template sul file tipi_esseri_viventi.xml -->
                            <xsl:apply-templates select="document('/home/localhero/Scrivania/Progetto/tipi_esseri_viventi.xml')//tipo">
                                <!-- Passo il parametro codTip al template -->
                                <xsl:with-param name="codTip" select="@codice_tipo"/>
                            </xsl:apply-templates>
                        </specie>
                        <!-- Creo il tag matrice -->
                        <matrice>
                            <!-- Qui andranno i tag di <affinità specie>%</affinità> -->
                        </matrice>
                        <!--THE TROUBLE IS THERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
                        <!-- Invoco template sul file forme.xml -->
                        <xsl:apply-templates select="document('/home/localhero/Scrivania/Progetto/forme.xml')//forma">
                            <!-- Passo il parametro codForm al template -->
                            <xsl:with-param name="codForm" select=".//forma/@codice"/>
                        </xsl:apply-templates>


                    </EV>
                </xsl:for-each>
            </snapshot>
        </xsl:for-each>
    </evoluzione>
</xsl:template>

<!-- Richiamo template su tutti i nodi EV del file esseri_viventi.xml-->
<xsl:template match="EV">
    <!-- Parametro codInd ricevuto in input dal template -->
    <xsl:param name="codInd"/>
    <!-- Se l'attributo Stringa non è NULL -->
    <xsl:if test="@Stringa != ''">
        <!-- Espressione regolare sull'attributo Stringa -->
        <xsl:analyze-string select="@Stringa" regex="(.*),(.*),(.*),(.*),(.*),(.*),(.*)">
            <xsl:matching-substring>
                <!-- Se il codice individuo coincide allora crea l'attributo aspettativa -->
                <xsl:if test="$codInd = regex-group(2)">
                    <xsl:attribute name="aspettativa">
                        <xsl:value-of select="regex-group(6)"/>
                    </xsl:attribute>
                </xsl:if>
            </xsl:matching-substring>
        </xsl:analyze-string>
    </xsl:if>
</xsl:template>

<!-- Richiamo template su tutti i nodi tipo del file esseri_viventi.xml-->
<xsl:template match="tipo">
    <!-- Parametro codTip ricevuto in input dal template -->
    <xsl:param name="codTip"/>
    <!-- Se il codice tipo coincide allora crea gli attributi -->
    <xsl:if test="$codTip = @codice">
        <xsl:attribute name="famiglia">
            <xsl:value-of select="../@nome"/>
        </xsl:attribute>
        <xsl:attribute name="nome_specie">
            <xsl:value-of select="@specie"/>
        </xsl:attribute>
    </xsl:if>
</xsl:template>

<!-- Richiamo template su tutti i nodi forma del file forme.xml-->
<xsl:template match="forma">
    <!-- Parametro codForm ricevuto in input dal template -->
    <xsl:param name="codForm"/>
    <!-- Se il codice forma coincide allora crea gli attributi -->
    <xsl:if test="$codForm = @codice">
        <!--<xsl:value-of select="$codForm"/><br/>-->
        <!-- Creo il tag forma -->
        <forma>
            <xsl:attribute name="tipo">
                <xsl:value-of select="../@tipo"/>
            </xsl:attribute>
            <xsl:attribute name="figura">
                <xsl:value-of select="@figura"/>
            </xsl:attribute>
            <!-- Ricorsione -->
            <xsl:apply-templates select="forma">
                <!-- Passo il parametro codForm al template -->
                <xsl:with-param name="codForm" select="document('/home/localhero/Scrivania/Progetto/evoluzione.xml')//forma/@codice"/>
            </xsl:apply-templates>
        </forma>
    </xsl:if>
</xsl:template>
</xsl:stylesheet>

我的结果:

   ...
   <EV>
        <forma tipo="quadrilatero" figura="rettangolo"></forma>
        <forma tipo="triangolo" figura="triangolo equilatero"></forma>
        <forma ...></forma>
        <forma ...></forma>
        ...
        <forma ...></forma>
    </EV>

为什么递归不起作用?为什么不以这种方式在递归模式中包含标签“forma”:

<forma>
  <forma>
     ...
  </forma>
</forma>

非常感谢您的耐心等待和帮助!

最佳答案

最简单的样式表看起来像这样:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:variable name="forme" select="document('/path/to/forme.xml')//forma" />

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="forma/@codice">
    <xsl:variable name="forma" select="$forme[@codice = current()]" />
    <xsl:copy-of select="$forma/@figura | $forma/../@tipo" />
  </xsl:template>
</xsl:stylesheet>

注释

  • 因为您想输出与 <EV> 相同的结构文档,您必须(嗯...应该)将样式表应用到那个文档,而不是查找文档。
  • “保持相同的结构”始终意味着从身份模板开始并根据需要覆盖它。
  • 在这种情况下,我们需要替换所有 codice <forma> 的属性元素,所以我们只需要一个匹配 forma/@codice 的模板并输出不同的属性。
  • $forme[@codice = current()]选择匹配 <forma>来自查找文档。
  • $forma/@figura | $forma/../@tipo选择我们需要的两个属性节点。
  • 解决方案假定 @codice数字在查找文档中是唯一的。

对于您的示例,输出如下所示:

<EV>
    <forma tipo="quadrilatero" figura="rettangolo">
        <!-- ... -->
        <forma tipo="triangolo" figura="triangolo equilatero">
            <!-- ... -->
            <forma>
                <!-- ... -->
            </forma>
        </forma>
    </forma>
</EV>

关于xml - XSLT - 递归模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19836747/

有关xml - XSLT - 递归模板的更多相关文章

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

  4. ruby - Chef Ruby 遍历 .erb 模板文件中的属性 - 2

    所以这可能有点令人困惑,但请耐心等待。简而言之,我想遍历具有特定键值的所有属性,然后如果值不为空,则将它们插入到模板中。这是我的代码:属性:#===DefaultfileConfigurations#default['elasticsearch']['default']['ES_USER']=''default['elasticsearch']['default']['ES_GROUP']=''default['elasticsearch']['default']['ES_HEAP_SIZE']=''default['elasticsearch']['default']['MAX_OP

  5. ruby - 递归地将所有数字字符串转换为 Ruby 哈希中的整数 - 2

    我有一个随机大小的散列,它可能有类似"100"的值,我想将其转换为整数。我知道我可以使用value.to_iifvalue.to_i.to_s==value来做到这一点,但我不确定我将如何在我的散列中递归地做到这一点,考虑到一个值可以是一个字符串,或一个数组(哈希或字符串),或另一个哈希。 最佳答案 这是一个非常简单的递归实现(尽管必须同时处理数组和散列会增加一些技巧)。deffixnumifyobjifobj.respond_to?:to_i#IfwecancastittoaFixnum,doit.obj.to_ielsifobj

  6. Ruby:标准递归模式 - 2

    我经常迷上ruby​​的一件事是递归模式。例如,假设我有一个数组,它可能包含无限深度的数组作为元素。所以,例如:my_array=[1,[2,3,[4,5,[6,7]]]]我想创建一个方法,可以将数组展平为[1,2,3,4,5,6,7]。我知道.flatten可以完成这项工作,但这个问题是作为我经常遇到的递归问题的一个例子-因此我试图找到一个更可重用的解决方案。简而言之-我猜这种事情有一个标准模式,但我想不出任何特别优雅的东西。任何想法表示赞赏 最佳答案 递归是一种方法,它不依赖于语言。您在编写算法时要考虑两种情况:再次调用函数的情

  7. ruby - 如何通过Middleman安装和使用Slim模板引擎 - 2

    一般来说,我是Middleman和ruby​​的新手。我已经安装了Ruby我已经安装了Middleman和gem以使其运行。我需要使用slim而不是默认的模板系统。所以我安装了Slimgem。Slim的网站只说我需要'slim'才能让它工作。中间人网站说我只需要在config.rb文件中添加模板引擎,但是没有给出例子...对于没有ruby​​背景的人来说,这没有帮助。我在git上找了几个config.rb,它们都有:require'slim'和#Setslim-langoutputstyleSlim::Engine.set_default_options:pretty=>true#Se

  8. ruby - 为什么我用递归得到 "stack level too deep"? - 2

    我有这个ruby代码:defget_sumnreturn0ifn似乎正在为999之前的值工作。当我尝试9999时,它给了我这个:stackleveltoodeep(SystemStackError)所以,我添加了这个:RubyVM::InstructionSequence.compile_option={:tailcall_optimization=>true,:trace_instruction=>false}但什么也没发生。我的ruby版本是:ruby1.9.3p392(2013-02-22revision39386)[x86_64-darwin12.2.1]我还增加了机器的堆栈大

  9. ruby-on-rails - 如何在 Rails 3 中禁用 XML 解析 - 2

    我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::

  10. ruby - 构建网络蜘蛛时,应该使用递归吗? - 2

    构建一个深度优先的网络蜘蛛,这意味着它将访问第一页上的所有链接,然后转到每个链接,并访问所有第二页上的链接...你应该使用递归吗?我发现这是CPU密集型的。defrecursion()linkz_on_first_page.eachdo|link|recursion(link)endendrecursion(firstpage) 最佳答案 绝对不是,由于万维网的实际性质,您很快就会遇到问题。当您访问带有主导航部分的网站时,每个页面都链接到其他页面,您就进入了一个无限循环。您可以跟踪您处理了哪些链接,但即便如此,递归循环并不真正适合万

随机推荐