Я хотел бы создать новые элементы. Каждый новый элемент должен содержать ключ записи, первый элемент кода и первое вхождение элемента Code_Text. Процесс будет повторяться для каждого из элементов Code и Code_Text, поскольку теоретически их должно быть одинаковое количество. Тот же XSL также будет применен к парам значений Opinion / Opinion_Text и Reason / Reason_Text.
Я использую предыдущее решение 1.0 из предыдущего вопроса, но не уверен, как его изменить. При тестировании у меня есть доступ только к версии 1.0. 2.0 доступен только на сервере. Мне нравится 2.0, поскольку это легче понять, но любой из них будет работать. В Java я бы просто l oop просматривал элементы, строил массив или список и затем выводил, когда закончил. Не уверен, как это сделать в XSL. Кроме того, в моем файле гораздо больше элементов, чем показано, и я предпочитаю обобщенное решение c, которое можно адаптировать к разным макетам XML файла. По сути, я хотел бы скопировать все, а затем l oop через пары значений, соединенные с ключом записи, выводя каждый в новый элемент с тем же именем. Я читал, что использование не является оптимальным решением, но не понимаю, почему в моем случае использования. Есть ли способ l oop просматривать данные, накапливать нужные значения и затем передавать их в выходной шаблон или это неверный путь к решению?
Input
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Record>
<Date>10/12/2019</Date>
<Code>F1</Code>
<Code>F2</Code>
<Code>F13</Code>
<Code>F9</Code>
<Code>F10</Code>
<Code>F11</Code>
<Code_Text>F1 Text</Code_Text>
<Code_Text>F2 Text</Code_Text>
<Code_Text>F13 Text</Code_Text>
<Code_Text>F9 Text</Code_Text>
<Code_Text>F10 Text</Code_Text>
<Code_Text>F11 Text</Code_Text>
<Key>12345</Key>
<Number>09095I</Number>
<Opinion>O1</Opinion>
<Opinion>O2</Opinion>
<Opinion>O3</Opinion>
<Opinion>O4</Opinion>
<Opinion>O5</Opinion>
<Opinion>O7</Opinion>
<Opinion_Text>O1 Text</Opinion_Text>
<Opinion_Text>O2 Text</Opinion_Text>
<Opinion_Text>O3 Text</Opinion_Text>
<Opinion_Text>O4 Text</Opinion_Text>
<Opinion_Text>O5 Text</Opinion_Text>
<Opinion_Text>O7 Text</Opinion_Text>
<Reason>R1</Reason>
<Reason>R2</Reason>
<Reason>R4</Reason>
<Reason>R3</Reason>
<Reason>R5</Reason>
<Reason>R6</Reason>
<Reason>R7</Reason>
<Reason>R8</Reason>
<Reason>R9</Reason>
<Reason_Text>R1 Text</Reason_Text>
<Reason_Text>R2 Text</Reason_Text>
<Reason_Text>R4 Text</Reason_Text>
<Reason_Text>R3 Text</Reason_Text>
<Reason_Text>R5 Text</Reason_Text>
<Reason_Text>R6 Text</Reason_Text>
<Reason_Text>R7 Text</Reason_Text>
<Reason_Text>R8 Text</Reason_Text>
<Reason_Text>R9 Text</Reason_Text>
<Summary>Record 1: Summary 1 Text</Summary>
<Summary>Record 2: Summary 1 Text</Summary>
<Text_Field_1>Record 1 Key 1: This is Text_Field_1</Text_Field_1>
<Text_Field_1>Record 2 Key 1: This is Text_Field_1</Text_Field_1>
<Text_Field_2>Record 1 Key 1: This is Text_Field_2</Text_Field_2>
<Text_Field_2>Record 2 Key 1: This is Text_Field_2</Text_Field_2>
</Record>
<Record>
<Date>01/12/2020</Date>
<Code>F12</Code>
<Code>F2</Code>
<Code>F3</Code>
<Code>F4</Code>
<Code>F5</Code>
<Code_Text>F12 Text</Code_Text>
<Code_Text>F2 Text</Code_Text>
<Code_Text>F3 Text</Code_Text>
<Code_Text>F4 Text</Code_Text>
<Code_Text>F5 Text</Code_Text>
<Key>23456</Key>
<Number>43095I</Number>
<Opinion>O8</Opinion>
<Opinion>O9</Opinion>
<Opinion>O1</Opinion>
<Opinion>O2</Opinion>
<Opinion>O4</Opinion>
<Opinion>O5</Opinion>
<Opinion>O6</Opinion>
<Opinion>O7</Opinion>
<Opinion_Text>O8 Text</Opinion_Text>
<Opinion_Text>O9 Text</Opinion_Text>
<Opinion_Text>O1 Text</Opinion_Text>
<Opinion_Text>O2 Text</Opinion_Text>
<Opinion_Text>O4 Text</Opinion_Text>
<Opinion_Text>O5 Text</Opinion_Text>
<Opinion_Text>O6 Text</Opinion_Text>
<Opinion_Text>O7 Text</Opinion_Text>
<Reason>R11</Reason>
<Reason>R4</Reason>
<Reason>R1</Reason>
<Reason>R2</Reason>
<Reason>R5</Reason>
<Reason>R6</Reason>
<Reason>R7</Reason>
<Reason>R8</Reason>
<Reason>R10</Reason>
<Reason>R9</Reason>
<Reason_Text>R11 Text</Reason_Text>
<Reason_Text>R4 Text</Reason_Text>
<Reason_Text>R1 Text</Reason_Text>
<Reason_Text>R2 Text</Reason_Text>
<Reason_Text>R5 Text</Reason_Text>
<Reason_Text>R6 Text</Reason_Text>
<Reason_Text>R7 Text</Reason_Text>
<Reason_Text>R8 Text</Reason_Text>
<Reason_Text>R10 Text</Reason_Text>
<Reason_Text>R9 Text</Reason_Text>
<Summary>Record 1: Summary 1 Text</Summary>
<Summary>Record 2: Summary 1 Text</Summary>
<Summary>Record 3: Summary 1 Text</Summary>
<Text_Field_1>Record 1 Key 2: This is Text Field 1</Text_Field_1>
<Text_Field_1>Record 2 Key 2: This is Text Field 1</Text_Field_1>
<Text_Field_1>Record 3 Key 2: This is Text_Field_1</Text_Field_1>
<Text_Field_1>Record 3 Key 2: This is Text_Field_2</Text_Field_1>
<Text_Field_2>Record 1 Key 2: This is Text_Field_2</Text_Field_2>
<Text_Field_2>Record 2 Key 2: This is Text_Field_2</Text_Field_2>
</Record>
<Record>
<Date>10/12/2019</Date>
<Code>F2</Code>
<Code>F3</Code>
<Code>F4</Code>
<Code>F5</Code>
<Code_Text>F2 Text</Code_Text>
<Code_Text>F3 Text</Code_Text>
<Code_Text>F4 Text</Code_Text>
<Code_Text>F5 Text</Code_Text>
<Key>778899</Key>
<Number>998822I</Number>
<Opinion>O1</Opinion>
<Opinion>O2</Opinion>
<Opinion>O3</Opinion>
<Opinion>O4</Opinion>
<Opinion>O5</Opinion>
<Opinion>O6</Opinion>
<Opinion>O7</Opinion>
<Opinion>O8</Opinion>
<Opinion>O9</Opinion>
<Opinion_Text>O1 Text</Opinion_Text>
<Opinion_Text>O2 Text</Opinion_Text>
<Opinion_Text>O3 Text</Opinion_Text>
<Opinion_Text>O4 Text</Opinion_Text>
<Opinion_Text>O5 Text</Opinion_Text>
<Opinion_Text>O6 Text</Opinion_Text>
<Opinion_Text>O7 Text</Opinion_Text>
<Opinion_Text>O8 Text</Opinion_Text>
<Opinion_Text>O9 Text</Opinion_Text>
<Reason>R1</Reason>
<Reason>R2</Reason>
<Reason_Text>R1 Text</Reason_Text>
<Reason_Text>R2 Text</Reason_Text>
<Summary>Record 1: Summary 1 Text</Summary>
<Text_Field_1>Record 3 Key 1: This is Text_Field_1</Text_Field_1>
<Text_Field_2>Record 3 Key 1: This is Text_Field_2</Text_Field_2>
</Record>
</Data>
Желаемый вывод
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Record>
<Date>10/12/2019</Date>
<Code>F1</Code>
<Code>F2</Code>
<Code>F13</Code>
<Code>F9</Code>
<Code>F10</Code>
<Code>F11</Code>
<Code_Text>F1 Text</Code_Text>
<Code_Text>F2 Text</Code_Text>
<Code_Text>F13 Text</Code_Text>
<Code_Text>F9 Text</Code_Text>
<Code_Text>F10 Text</Code_Text>
<Code_Text>F11 Text</Code_Text>
<Code_Combined>12345|F1|F1 Text</Code_Combined>
<Code_Combined>12345|F2|F2 Text</Code_Combined>
<Code_Combined>12345|F13|F13 Text</Code_Combined>
<Code_Combined>12345|F9|F9 Text</Code_Combined>
<Code_Combined>12345|F10|F10 Text</Code_Combined>
<Code_Combined>12345|F11|F11 Text</Code_Combined>
<Key>12345</Key>
<Number>09095I</Number>
<Opinion>O1</Opinion>
<Opinion>O2</Opinion>
<Opinion>O3</Opinion>
<Opinion>O4</Opinion>
<Opinion>O5</Opinion>
<Opinion>O7</Opinion>
<Opinion_Text>O1 Text</Opinion_Text>
<Opinion_Text>O2 Text</Opinion_Text>
<Opinion_Text>O3 Text</Opinion_Text>
<Opinion_Text>O4 Text</Opinion_Text>
<Opinion_Text>O5 Text</Opinion_Text>
<Opinion_Text>O7 Text</Opinion_Text>
<Opinion_Combined>12345|O1|O1 Text</Opinion_Combined>
<Opinion_Combined>12345|O2|O2 Text</Opinion_Combined>
<Opinion_Combined>12345|O3|O3 Text</Opinion_Combined>
<Opinion_Combined>12345|O4|O4 Text</Opinion_Combined>
<Opinion_Combined>12345|O5|O5 Text</Opinion_Combined>
<Opinion_Combined>12345|O7|O7 Text</Opinion_Combined>
<Reason>R1</Reason>
<Reason>R2</Reason>
<Reason>R4</Reason>
<Reason>R3</Reason>
<Reason>R5</Reason>
<Reason>R6</Reason>
<Reason>R7</Reason>
<Reason>R8</Reason>
<Reason>R9</Reason>
<Reason_Text>R1 Text</Reason_Text>
<Reason_Text>R2 Text</Reason_Text>
<Reason_Text>R4 Text</Reason_Text>
<Reason_Text>R3 Text</Reason_Text>
<Reason_Text>R5 Text</Reason_Text>
<Reason_Text>R6 Text</Reason_Text>
<Reason_Text>R7 Text</Reason_Text>
<Reason_Text>R8 Text</Reason_Text>
<Reason_Text>R9 Text</Reason_Text>
<Reason_Combined>12345|R1|R1 Text</Reason_Combined>
<Reason_Combined>12345|R2|R2 Text</Reason_Combined>
<Reason_Combined>12345|R4|R4 Text</Reason_Combined>
<Reason_Combined>12345|R3|R3 Text</Reason_Combined>
<Reason_Combined>12345|R5|R5 Text</Reason_Combined>
<Reason_Combined>12345|R6|R6 Text</Reason_Combined>
<Reason_Combined>12345|R7|R7 Text</Reason_Combined>
<Reason_Combined>12345|R8|R8 Text</Reason_Combined>
<Reason_Combined>12345|R9|R9 Text</Reason_Combined>
<Summary>Record 1: Summary 1 Text</Summary>
<Summary>Record 2: Summary 1 Text</Summary>
<Text_Field_1>Record 1 Key 1: This is Text_Field_1</Text_Field_1>
<Text_Field_1>Record 2 Key 1: This is Text_Field_1</Text_Field_1>
<Text_Field_2>Record 1 Key 1: This is Text_Field_2</Text_Field_2>
<Text_Field_2>Record 2 Key 1: This is Text_Field_2</Text_Field_2>
</Record>
<Record>
<Date>01/12/2020</Date>
<Code>F12</Code>
<Code>F2</Code>
<Code>F3</Code>
<Code>F4</Code>
<Code>F5</Code>
<Code_Text>F12 Text</Code_Text>
<Code_Text>F2 Text</Code_Text>
<Code_Text>F3 Text</Code_Text>
<Code_Text>F4 Text</Code_Text>
<Code_Text>F5 Text</Code_Text>
<Code_Combined>23456|F12|F12 Text</Code_Combined>
<Code_Combined>23456|F2|F2 Text</Code_Combined>
<Code_Combined>23456|F3|F3 Text</Code_Combined>
<Code_Combined>23456|F4|F4 Text</Code_Combined>
<Code_Combined>23456|F5|F5 Text</Code_Combined>
<Key>23456</Key>
<Number>43095I</Number>
<Opinion>O8</Opinion>
<Opinion>O9</Opinion>
<Opinion>O1</Opinion>
<Opinion>O2</Opinion>
<Opinion>O4</Opinion>
<Opinion>O5</Opinion>
<Opinion>O6</Opinion>
<Opinion>O7</Opinion>
<Opinion_Text>O8 Text</Opinion_Text>
<Opinion_Text>O9 Text</Opinion_Text>
<Opinion_Text>O1 Text</Opinion_Text>
<Opinion_Text>O2 Text</Opinion_Text>
<Opinion_Text>O4 Text</Opinion_Text>
<Opinion_Text>O5 Text</Opinion_Text>
<Opinion_Text>O6 Text</Opinion_Text>
<Opinion_Text>O7 Text</Opinion_Text>
<Opinion_Combined>23456|O8|O8 Text</Opinion_Combined>
<Opinion_Combined>23456|O9|O9 Text</Opinion_Combined>
<Opinion_Combined>23456|O1|O1 Text</Opinion_Combined>
<Opinion_Combined>23456|O2|O2 Text</Opinion_Combined>
<Opinion_Combined>23456|O4|O4 Text</Opinion_Combined>
<Opinion_Combined>23456|O5|O5 Text</Opinion_Combined>
<Opinion_Combined>23456|O6|O6 Text</Opinion_Combined>
<Opinion_Combined>23456|O7|O7 Text</Opinion_Combined>
<Reason>R11</Reason>
<Reason>R4</Reason>
<Reason>R1</Reason>
<Reason>R2</Reason>
<Reason>R5</Reason>
<Reason>R6</Reason>
<Reason>R7</Reason>
<Reason>R8</Reason>
<Reason>R10</Reason>
<Reason>R9</Reason>
<Reason_Text>R11 Text</Reason_Text>
<Reason_Text>R4 Text</Reason_Text>
<Reason_Text>R1 Text</Reason_Text>
<Reason_Text>R2 Text</Reason_Text>
<Reason_Text>R5 Text</Reason_Text>
<Reason_Text>R6 Text</Reason_Text>
<Reason_Text>R7 Text</Reason_Text>
<Reason_Text>R8 Text</Reason_Text>
<Reason_Text>R10 Text</Reason_Text>
<Reason_Text>R9 Text</Reason_Text>
<Reason_Combined>23456|R11|R11 Text</Reason_Combined>
<Reason_Combined>23456|R4|R4 Text</Reason_Combined>
<Reason_Combined>23456|R1|R1 Text</Reason_Combined>
<Reason_Combined>23456|R2|R2 Text</Reason_Combined>
<Reason_Combined>23456|R5|R5 Text</Reason_Combined>
<Reason_Combined>23456|R6|R6 Text</Reason_Combined>
<Reason_Combined>23456|R7|R7 Text</Reason_Combined>
<Reason_Combined>23456|R8|R8 Text</Reason_Combined>
<Reason_Combined>23456|R10|R10 Text</Reason_Combined>
<Reason_Combined>23456|R9|R9 Text</Reason_Combined>
<Summary>Record 1: Summary 1 Text</Summary>
<Summary>Record 2: Summary 1 Text</Summary>
<Summary>Record 3: Summary 1 Text</Summary>
<Text_Field_1>Record 1 Key 2: This is Text Field 1</Text_Field_1>
<Text_Field_1>Record 2 Key 2: This is Text Field 1</Text_Field_1>
<Text_Field_1>Record 3 Key 2: This is Text_Field_1</Text_Field_1>
<Text_Field_1>Record 3 Key 2: This is Text_Field_2</Text_Field_1>
<Text_Field_2>Record 1 Key 2: This is Text_Field_2</Text_Field_2>
<Text_Field_2>Record 2 Key 2: This is Text_Field_2</Text_Field_2>
</Record>
<Record>
<Date>10/12/2019</Date>
<Code>F2</Code>
<Code>F3</Code>
<Code>F4</Code>
<Code>F5</Code>
<Code_Text>F2 Text</Code_Text>
<Code_Text>F3 Text</Code_Text>
<Code_Text>F4 Text</Code_Text>
<Code_Text>F5 Text</Code_Text>
<Code_Combined>778899|F2|F2 Text</Code_Combined>
<Code_Combined>778899|F3|F3 Text</Code_Combined>
<Code_Combined>778899|F4|F4 Text</Code_Combined>
<Code_Combined>778899|F5|F5 Text</Code_Combined>
<Key>778899</Key>
<Number>998822I</Number>
<Opinion>O1</Opinion>
<Opinion>O2</Opinion>
<Opinion>O3</Opinion>
<Opinion>O4</Opinion>
<Opinion>O5</Opinion>
<Opinion>O6</Opinion>
<Opinion>O7</Opinion>
<Opinion>O8</Opinion>
<Opinion>O9</Opinion>
<Opinion_Text>O1 Text</Opinion_Text>
<Opinion_Text>O2 Text</Opinion_Text>
<Opinion_Text>O3 Text</Opinion_Text>
<Opinion_Text>O4 Text</Opinion_Text>
<Opinion_Text>O5 Text</Opinion_Text>
<Opinion_Text>O6 Text</Opinion_Text>
<Opinion_Text>O7 Text</Opinion_Text>
<Opinion_Text>O8 Text</Opinion_Text>
<Opinion_Text>O9 Text</Opinion_Text>
<Opinion_Combined>778899|O1|O1 Text</Opinion_Combined>
<Opinion_Combined>778899|O2|O2 Text</Opinion_Combined>
<Opinion_Combined>778899|O3|O3 Text</Opinion_Combined>
<Opinion_Combined>778899|O4|O4 Text</Opinion_Combined>
<Opinion_Combined>778899|O5|O5 Text</Opinion_Combined>
<Opinion_Combined>778899|O6|O6 Text</Opinion_Combined>
<Opinion_Combined>778899|O7|O7 Text</Opinion_Combined>
<Opinion_Combined>778899|O8|O8 Text</Opinion_Combined>
<Opinion_Combined>778899|O9|O9 Text</Opinion_Combined>
<Reason>R1</Reason>
<Reason>R2</Reason>
<Reason_Text>R1 Text</Reason_Text>
<Reason_Text>R2 Text</Reason_Text>
<Reason_Combined>778899|R1|R1 Text</Reason_Combined>
<Reason_Combined>778899|R2|R2 Text</Reason_Combined>
<Summary>Record 1: Summary 1 Text</Summary>
<Text_Field_1>Record 3 Key 1: This is Text_Field_1</Text_Field_1>
<Text_Field_2>Record 3 Key 1: This is Text_Field_2</Text_Field_2>
</Record>
</Data>
Оригинальный код
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
<Combined>
<xsl:for-each select="*[not(self::Date)]">
<xsl:choose>
<xsl:when test="name() = name(preceding-sibling::*[1])">, </xsl:when>
<xsl:when test="position() = 1" />
<xsl:otherwise>|</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="." />
</xsl:for-each>
</Combined>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>