请您提供有关如何将节点插入我的XML文件的帮助和建议,其中此节点将具有以下内部值:当前节点与上一节点之间的差异(仅一个先前数据)。
我的输入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>
.
.
.
.
.
我的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>
我的输出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>
.
.
.
.
.
我的预期输出是:
<?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>
.
.
.
.
.
我无法让1部分正确。预期内部值= OriIndex(当前位置) - OriIndex(先前位置)或OriIndex(位置n) - OriIndex(位置n-1)。
请帮我解决这个问题。 非常感谢您的努力。
非常感谢。
干杯!
答案 0 :(得分:0)
我使用了xml linq。您希望输出不是使用与输入xml相同的xml,因此我的结果与您的结果不匹配。我认为我的代码是对的。
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;
}
}
}
}