2017-09-26 38 views
0

如果有2个节点,请您帮我减去内部文本。产生如何在XSLT中减去两个节点的内部文本

我的XML是:(原始XML是直到OriIndex)

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<Root> 
    <Test> 
    <TestPhase>1</TestPhase> 
    <TestFlow>1</TestFlow> 
    <TestParameter>1</TestParameter> 
    <OriIndex>0</OriIndex> 
    <SortedIndex>0</SortedIndex> 
    <Diff>NaN</Diff> 
    </Test> 
. 
. 
. 
. 
. 

我不能够得到DIFF。例如它应该是<SortedIndex>10</SortedIndex> - <OriIndex>5</OriIndex> Equals 5。

我的XSLT是:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

<xsl:output method="xml" encoding = "UTF-8" indent="yes" omit-xml-declaration="no" standalone="yes" /> 


    <xsl:template match="Root"> 
    <xsl:copy> 
    <xsl:apply-templates select="Test"> 
     <xsl:sort select="TestPhase" data-type="number" order="ascending"/> 
     <xsl:sort select="TestFlow" data-type="number" order="ascending"/> 
     <xsl:sort select="TestParameter" data-type="number" order="ascending"/> 
    </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 


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


    <xsl:template match="Test"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | *"/> 
     <SortedIndex><xsl:value-of select="position() - 1"/></SortedIndex> 
     <Diff><xsl:value-of select="@SortedIndex - @OriIndex" /></Diff> 
    </xsl:copy> 
    </xsl:template> 


</xsl:stylesheet> 

请帮助。非常感谢您的努力。

非常感谢。

干杯, newbuntu

回答

0

好,<Diff><xsl:value-of select="@SortedIndex - @OriIndex" /></Diff>将计算该名称的属性之间的区别,你没有属性。如果你想计算子元素的差异,你需要<Diff><xsl:value-of select="SortedIndex - OriIndex" /></Diff>。但是,您不清楚的描述的一部分听起来好像您在原始输入中没有SortedIndex,但只是使用XSLT创建结果,在这种情况下,您需要<Diff><xsl:value-of select="position() - 1 - OriIndex" /></Diff>

+0

非常感谢Martin Honnen。它工作正常。我其实已经错过了一个逻辑。你能请教我如何编写代码。我想要了解当前OriIndex内部文本和以前的OriIndex内部文本(仅在此之前的文本)之间的区别。我把:,但它有编译错误。请帮忙。干杯:) – newbuntu