Я изо всех сил пытаюсь преобразовать XML-файл excel в другой формат xml.excel xml to xml - xslt для итерации внутреннего цикла
Вот упрощенный экстракт источника:
<Workbook>
<Worksheet>
<Table>
<Row>
<Cell>
<Data>Test 1</Data>
</Cell>
<Cell>
<Data>Preconditions for test 1</Data>
</Cell>
<Cell>
<Data>The setup for test 1</Data>
</Cell>
<Cell />
</Row>
<Row>
<Cell />
<Cell>
<Data>Step 1</Data>
</Cell>
<Cell>
<Data>Todo in step 1</Data>
</Cell>
<Cell>
<Data>Expected result</Data> <!--omitted if empty-->
</Cell>
</Row>
.
.
<Row>
<Cell />
<Cell>
<Data>Step n</Data>
</Cell>
<Cell>
<Data>Todo in step n</Data>
</Cell>
<Cell>
<Data>Expected result</Data> <!--omitted if empty-->
</Cell>
</Row>
.
.
-----------------
.
.
<Row>
<Cell>
<Data>Test m</Data>
</Cell>
<Cell>
<Data>Preconditions for test m</Data>
</Cell>
<Cell>
<Data>The setup for test m</Data>
</Cell>
<Cell />
</Row>
<Row>
<Cell />
<Cell>
<Data>Step 1</Data>
</Cell>
<Cell>
<Data>Todo in step 1</Data>
</Cell>
<Cell />
<Cell>
<Data>Expected result</Data> <!--omitted if empty-->
</Cell>
</Row>
.
.
<Row>
<Cell />
<Cell>
<Data>Step k</Data>
</Cell>
<Cell>
<Data>Todo in step k</Data>
</Cell>
<Cell>
<Data>Expected result</Data> <!--omitted if empty-->
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
Колонка 2 (Ячейка [2]) можно рассматривать в качестве заголовка для перевалы 3-4
Моя требуемый выход должен быть в следующем формат:
<case>
<title>Test 1</title>
<precond>The setup for test 1</precond>
<step>
<index>1</index>
<content>Todo in step 1</content>
<expected>Expected result</expected> <!--omitted if empty-->
</step>
.
.
<step>
<index>n</index>
<content>Todo in step n</content>
<expected>Expected result</expected> <!--omitted if empty-->
</step>
</case>
.
.
.......
.
.
<case>
<title>Test m</title>
<precond>The setup for test m</precond>
<step>
<index>1</index>
<content>Todo in step 1</content>
<expected>Expected result</expected> <!--omitted if empty-->
</step>
.
.
<step>
<index>n</index>
<content>Todo in step n</content>
<expected>Expected result</expected> <!--omitted if empty-->
</step>
</case>
Моя проблема заключается в определении XSLT, так что <case>
теги не охватывают все строки до следующей строки, которая имеет ввод данных в ячейке [1]. Если я использую <xsl:if test="Cell[1]/Data">
, чтобы найти следующий тест, закрытие </case>
должно быть введено до </xsl:if>
, и поэтому мне нужно выполнить итерацию следующих строк (шагов теста) в этом выражении. Как это сделать?
Вот моя слабая попытка на XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:template match="Workbook/Worksheet/Table">
<xsl:for-each select="Row">
<xsl:if test="Cell[1]/Data">
<case>
<title>
<xsl:value-of select="Cell[1]/Data"/>
</title>
<xsl:if test="Cell[3]/Data">
<precond>
<xsl:value-of select="Cell[3]/Data"/>
</precond>
</xsl:if>
<!-- Here I need to iterate "while not" following-sibling::Cell[1]/Data and extract the data from cells 2-4.-->
</case>
</xsl:if>
</xsl:for-each>
</xsl:template>
Нужно ли быть XSLT 1.0 или у вас есть возможность использовать XSLT 2.0? –
Марк, я действительно не рассматривал различия. Я тоже не очень хорошо знаком. Я сделал небольшую программу на C#, используя System.Xml.Xsl как движок в .net. Из [здесь] (http://stackoverflow.com/questions/1525299/xpath-and-xslt-2-0-for-net) кажется, что требуется xslt 1.0 – user2964512
Также добавлено решение XSLT 1.0. –