比较两个列表和输出在XSLT 1.0的差异 [英] Comparing two lists and outputting differences in XSLT 1.0

查看:109
本文介绍了比较两个列表和输出在XSLT 1.0的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经重新开始这个,我试图让我的头一轮的这个基础,并都失败了,下面是结构的XML将最有可能采取。然后我需要输出低于...

I have started this again, i was trying to get my head round the basics of this and have failed, below is the structure the XML will most likely take. and then what i need to output below that...

 <rtpm>
    <old>
        <simple>
            <information>
                <name1code>AAA</name1code>
                <name1use>N</name1use>
                <name2code>BBB</name2code>
                <name2use>P</name2use>
                <name3code>CCC</name3code>
                <name3use>N</name3use>
                <name4code>DDD</name4code>
                <name4use>N</name4use>
                <name5code>EEE</name5code>
                <name5use>N</name5use>
            </information>  
        </simple>
    </old>
    <new>
        <simple>
            <information>
                <name1code>AAA</name1code>
                <name1use>N</name1use>
                <name2code>BBC</name2code>
                <name2use>P</name2use>
                <name3code>AFD</name3code>
                <name3use>N</name3use>
                <name4code>CCC</name4code>
                <name4use>N</name4use>
                <name5code>EEE</name5code>
                <name5use>N</name5use>
            </information>
        </simple>
    </new>
</rtpm>

我会再需要它进行什么previously描述和输出

I would then need this to perform what was previously described and output as

<request>
    <query0>BBC</query0>
    <use0>P</use0>
    <query1>AFD</query1>
    <use1>N</use1>
</request>

在code目前使用的是按以下,但我不知道如何使它只要拿起名1 code,2 code等旧钥匙
当一个'不'找到匹配为code使用刚刚拿起的使用价值。

The code currently in use is as per below but i have no idea how to make it just pick up name1code, 2code etc for the old key and the use to just pick up the use value when a 'not' match is found for the code.

  <xsl:key name="old" match="old/simple/information/*" use="." />
<xsl:key name="use" match="new/simple/information/*" use="name()" />

<xsl:template match="/*">
<request>
    <xsl:for-each select="new/simple/information/*[not(key('old', .))]">
        <xsl:element name="query{position() - 1}">
            <xsl:value-of select="." />
        </xsl:element>
        <xsl:element name="use{position() - 1}">
            <xsl:value-of select="key('use', concat(name(), 'use'))" /> <!--2-->        
        </xsl:element>
    </xsl:for-each>
</request>
</xsl:template>

&LT;! - 2 - 方式&gt; 是行中我不能左右我的头

<!--2--> is the line i can't get my head around.

感谢您过所有迄今已帮助了,你一直是一个巨大的帮助。我说我有没有让我的头一轮,但我得到了什么/是如何工作在一定程度上,只是没有如何进一步吧。

Thank you too all that have helped so far, you have been a huge help. i say i have failed getting my head round it, but i get what/how this works to a degree, just not how to further it.

推荐答案

一种方法是使用一键查找旧值

One way is to use a key to look up the old values

<xsl:key name="old" match="old/*" use="text()" />

然后,选择与值的元素不发生在元素的列表,你根本就这个(假设你被定位在 RTPM

Then, to select new elements with values don't occur in the list of old elements, you would simply do this (assuming you were positioned on rtpm)

<xsl:apply-templates select="new/*[not(key('old', text()))]" />

而在匹配该模板,创建相关的查询元素,你会利用在位置()功能( 的这是相对于你刚才选择的节点,而不是在层次结构其中的位置

And in the template that matches this, to create the relevant query element, you would make use of the position() function (as this is relative to the nodes you have just selected, not the position of them in the hierarchy)

 <xsl:element name="query{position() - 1}">
   <xsl:value-of select="text()" />
 </xsl:element>

试试这个XSLT

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output omit-xml-declaration="yes" indent="yes" />
   <xsl:key name="old" match="old/*" use="text()" />
   <xsl:template match="/*">
     <xsl:apply-templates select="new/*[not(key('old', text()))]" />
   </xsl:template>

   <xsl:template match="new/*">
     <xsl:element name="query{position() - 1}">
       <xsl:value-of select="text()" />
     </xsl:element>
   </xsl:template>
</xsl:stylesheet>

当你的第一个XML应用示例,下面是输出

When applied on your first XML sample, the following is output

<query0>ABB</query0>
<query1>ACD</query1>

在第二种情况下,以下也被输出

In the second case, the following is also output

<query0>ABB</query0>
<query1>ACD</query1>

编辑:这个问题已经有所改变,因为它最初被问(它确实应该已经被要求作为一个新的问题),但要回答这个新问题,在EX pression &LT;! - 2 - &GT; 您正在寻找的是这样的。

The question has changed somewhat since it was originally asked (It should really have been asked as a new question), but to answer this new question, the expression in <!-- 2 --> you are looking for is this

<xsl:value-of select="key('use', concat(substring-before(name(), 'code'), 'use'))" />

这是因为在当前节点有nameX code的名称,它需要被改变成nameXuse来查找它的关键。所以,才把code的文本,然后追加上月底,而不是使用。

This is because the current node has a name of "nameXcode", and it needs to be changed to "nameXuse" to look it up in the key. So, you get the text before "code" and then append "use" on the end instead.

但是,如果使用你得到节点总是将是以下同级到 code 节点,你可能只是做到这一点。

However, if the use node you are getting is always going to be the following sibling to the code node, you could just do this

<xsl:value-of select="following-sibling::*[1]" />

这会起作用,因为以下同胞会在层次以下的兄弟为你在当前节点,而不是你刚才选择的节点下一个。

This will work because following-sibling will get the following sibling in the hierarchy for the current node you are on, and not the next one in the nodes you have just selected.

这篇关于比较两个列表和输出在XSLT 1.0的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆