2017-09-26 39 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