[英]How to find the difference of present node (n) and previous node (n-1)
Would you please help and advice on how to insert a node into my XML file, whereby this node will have the inner value of : difference between current node and previous node (just one previous data). 请提供有关如何将节点插入我的XML文件的帮助和建议,由此该节点将具有以下内部值:当前节点与上一个节点之间的差(仅一个先前的数据)。
My input XML is: 我的输入XML是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>1</TestFlow>
<TestParameter>1</TestParameter>
<OriIndex>0</OriIndex>
</Test>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>1</TestFlow>
<TestParameter>2</TestParameter>
<OriIndex>1</OriIndex>
</Test>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>3</TestFlow>
<TestParameter>1</TestParameter>
<OriIndex>2</OriIndex>
</Test>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>2</TestFlow>
<TestParameter>2</TestParameter>
<OriIndex>3</OriIndex>
</Test>
<Test>
<TestPhase>2</TestPhase>
<TestFlow>1</TestFlow>
<TestParameter>1</TestParameter>
<OriIndex>4</OriIndex>
</Test>
.
.
.
.
.
My XSLT is : 我的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>
<DiffSortedIndex><xsl:value-of select="position() - 1 - OriIndex" /></DiffSortedIndex>
<DiffOriIndex><xsl:value-of select="OriIndex - preceding-sibling::Test[position()-1]/OriIndex" /></DiffOriIndex>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
My output XML is : 我的输出XML是:
<?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>
<DiffSortedIndex>0</DiffSortedIndex>
<DiffOriIndex>NaN</DiffOriIndex>
</Test>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>1</TestFlow>
<TestParameter>2</TestParameter>
<OriIndex>1</OriIndex>
<SortedIndex>1</SortedIndex>
<DiffSortedIndex>0</DiffSortedIndex>
<DiffOriIndex>NaN</DiffOriIndex>
</Test>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>2</TestFlow>
<TestParameter>2</TestParameter>
<OriIndex>3</OriIndex>
<SortedIndex>2</SortedIndex>
<DiffSortedIndex>-1</DiffSortedIndex>
<DiffOriIndex>NaN</DiffOriIndex>
</Test>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>2</TestFlow>
<TestParameter>2</TestParameter>
<OriIndex>12</OriIndex>
<SortedIndex>3</SortedIndex>
<DiffSortedIndex>-9</DiffSortedIndex>
<DiffOriIndex>NaN</DiffOriIndex>
</Test>
.
.
.
.
.
My Expected output is : 我的预期输出是:
<?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>
<DiffSortedIndex>0</DiffSortedIndex>
<DiffOriIndex>NaN</DiffOriIndex>
</Test>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>1</TestFlow>
<TestParameter>2</TestParameter>
<OriIndex>1</OriIndex>
<SortedIndex>1</SortedIndex>
<DiffSortedIndex>0</DiffSortedIndex>
<DiffOriIndex>1</DiffOriIndex>
</Test>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>2</TestFlow>
<TestParameter>2</TestParameter>
<OriIndex>3</OriIndex>
<SortedIndex>2</SortedIndex>
<DiffSortedIndex>-1</DiffSortedIndex>
<DiffOriIndex>2</DiffOriIndex>
</Test>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>2</TestFlow>
<TestParameter>2</TestParameter>
<OriIndex>12</OriIndex>
<SortedIndex>3</SortedIndex>
<DiffSortedIndex>-9</DiffSortedIndex>
<DiffOriIndex>9</DiffOriIndex>
</Test>
<Test>
<TestPhase>1</TestPhase>
<TestFlow>3</TestFlow>
<TestParameter>1</TestParameter>
<OriIndex>2</OriIndex>
<SortedIndex>4</SortedIndex>
<DiffSortedIndex>2</DiffSortedIndex>
<DiffOriIndex>-10</DiffOriIndex>
</Test>
<Test>
<TestPhase>2</TestPhase>
<TestFlow>1</TestFlow>
<TestParameter>1</TestParameter>
<OriIndex>4</OriIndex>
<SortedIndex>5</SortedIndex>
<DiffSortedIndex>1</DiffSortedIndex>
<DiffOriIndex>2</DiffOriIndex>
</Test>
<Test>
<TestPhase>2</TestPhase>
<TestFlow>1</TestFlow>
<TestParameter>2</TestParameter>
<OriIndex>5</OriIndex>
<SortedIndex>6</SortedIndex>
<DiffSortedIndex>1</DiffSortedIndex>
<DiffOriIndex>1</DiffOriIndex>
</Test>
<Test>
<TestPhase>2</TestPhase>
<TestFlow>2</TestFlow>
<TestParameter>1</TestParameter>
<OriIndex>6</OriIndex>
<SortedIndex>7</SortedIndex>
<DiffSortedIndex>1</DiffSortedIndex>
<DiffOriIndex>1</DiffOriIndex>
</Test>
<Test>
<TestPhase>2</TestPhase>
<TestFlow>2</TestFlow>
<TestParameter>2</TestParameter>
<OriIndex>7</OriIndex>
<SortedIndex>8</SortedIndex>
<DiffSortedIndex>1</DiffSortedIndex>
<DiffOriIndex>1</DiffOriIndex>
</Test>
.
.
.
.
.
I am not able to get the 1 part correct. 我无法正确理解第1部分。 The expected inner value = OriIndex (present position) - OriIndex (previous position) or OriIndex (position n) - OriIndex (position n-1). 期望的内部值= OriIndex(当前位置)-OriIndex(先前位置)或OriIndex(位置n)-OriIndex(位置n-1)。
Please help me on this. 请帮我。 Your effort is very much appreciated. 非常感谢您的努力。
Thank you very much. 非常感谢你。
Cheers! 干杯!
I used xml linq. 我使用xml linq。 You expected output isn't using the same xml as your input xml so my results aren't matching yours. 您期望的输出没有使用与输入xml相同的xml,因此我的结果与您的结果不匹配。 I think my code is right. 我认为我的代码是正确的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> tests = doc.Descendants("Test").OrderBy(x => (int)x.Element("OriIndex")).ToList();
int previousOriIndex = 0;
for (int i = 0; i < tests.Count; i++)
{
XElement test = tests[i];
int currentOriIndex = (int)test.Element("OriIndex");
test.Add(new XElement("SortedIndex", i));
test.Add(new XElement("DiffSortedIndex", i - currentOriIndex));
if (i == 0)
{
test.Add(new XElement("DiffOriIndex", "NaN"));
}
else
{
test.Add(new XElement("DiffOriIndex", currentOriIndex - previousOriIndex));
}
previousOriIndex = currentOriIndex;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.