简体   繁体   English

如何检查xslt中的上一个节点值?

[英]How to check the previous node value in xslt?

I have this xml where I'm getting repeated node set.I have to print it just one time. 我有这个XML,我正在重复节点集。我只需要打印一次。 So I was thinking to compare the value with previous node set value and if it doesn't match then print it. 因此,我正在考虑将该值与先前的节点设置值进行比较,如果不匹配,则将其打印出来。

this is my code:- 这是我的代码:

<xsl:for-each select="Details">
<xsl:variable name="pos" select="position()"/>
    <xsl:if test="preceding-sibling::Details/Value[$pos] !=current()">
    <xsl:variable name="sign" select="'Y'"/>
    </xsl:if> 
</xsl:for>

Input XML:- 输入XML:

<Details>
    <Value>10</Value>
    <name>A</name>
</Details>
<Details>
    <Value>10</Value>
    <name>A</name> 
</Details>
<Details>
    <Value>20</Value>
    <name>B</name> 
</Details>
<Details>
    <Value>20</Value>
    <name>B</name> 
</Details>
<Details>
    <Value>30</Value>
    <name>C</name> 
</Details>
<Details>
    <Value>30</Value>
    <name>C</name> 
</Details>
<Details>
    <Value>30</Value>
    <name>C</name> 
</Details>

My output should be :- 我的输出应该是:

<Details>
    <Value>10</Value>
    <name>A</name>
</Details>
<Details>
    <Value>20</Value>
    <name>B</name> 
</Details>
<Details>
    <Value>30</Value>
    <name>C</name> 
</Details>

So here it compares the value of 所以在这里它比较了

<Value> 

of current node with previous 'Value'. 具有先前“值”的当前节点。 If it is unequal ex: 10<>20 then it should print 20s node set 如果不相等,例如:10 <> 20,则应打印20s节点集

Please help me with correct usage of preceding sibling or any other alternate. 请帮助我正确使用前面的兄弟姐妹或任何其他替代方法。 please note that the value of is not static.it might change in other xml so no hard coding. 请注意,的值不是static。它可能会在其他xml中更改,因此无需进行硬编码。 Thanks in advance 提前致谢

To implement your logic, you could simply do: 要实现您的逻辑,您可以简单地执行以下操作:

<xsl:copy-of select="Details[not(Value = preceding-sibling::Details[1]/Value)]"/>

However, this assumes the original XML is sorted; 但是,这假设原始XML已排序; otherwise you will get duplicates when the same value appears again outside its first block. 否则,当相同的值再次出现在其第一个块之外时,您将得到重复项。 If that's not what you want, then use Muenchian grouping instead - it will be more efficient, too. 如果这不是您想要的,请改为使用Muenchian分组 -这样也会更有效。

I am pretty sure that you are searching this: 我很确定您正在搜索以下内容:

<xsl:for-each-group select="Details" group-by=".">
    <xsl:copy-of select="."/>
</xsl:for-each-group>

Alternative to this would be something like this How to select unique nodes 替代方法可能是这样的: 如何选择唯一节点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM