Прежде всего, если вам нужно работать с XSLT, я предлагаю вам прочитать несколько руководств, которые помогут вам понять синтаксис XSLT. Например, учебники по W3Schools.
Вот XSLT, который дает необходимый результат:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-16" indent="yes"/>
<!-- Identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- When <count> equals 3, create to extra <item> elements, with the same value of this current <item>, but default <count> to 1 -->
<xsl:template match="item[count=3]">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
<item>
<count>1</count>
<name><xsl:value-of select="name" /></name>
<description><xsl:value-of select="description" /></description>
</item>
<item>
<count>1</count>
<name><xsl:value-of select="name" /></name>
<description><xsl:value-of select="description" /></description>
</item>
</xsl:template>
<!-- Reset the <count>3</count> element to value 1 -->
<xsl:template match="count[text()=3]">
<xsl:copy>
<xsl:text>1</xsl:text>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Если вы хотите сделать все рекурсивно и independed на <count>
элемент, но всегда выполнять его, когда <count>
больше, чем 1 , затем используйте следующий шаблон:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- Identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- When <count> is greater than 1, duplicate the <item> elements occording to the <count>, with the same value of this current <item>, but default <count> to 1 -->
<xsl:template match="item[count > 1]">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
<xsl:call-template name="multiply">
<xsl:with-param name="count" select="count" />
<xsl:with-param name="name" select="name" />
<xsl:with-param name="description" select="description" />
</xsl:call-template>
</xsl:template>
<!-- Reset the <count> element to value 1, when the <count> element is greater than 1 -->
<xsl:template match="count[text() > 1]">
<xsl:copy>
<xsl:text>1</xsl:text>
</xsl:copy>
</xsl:template>
<!-- Recursive template -->
<xsl:template name="multiply">
<xsl:param name="count" />
<xsl:param name="name" />
<xsl:param name="description" />
<xsl:if test="$count > 1">
<item>
<count>1</count>
<name><xsl:value-of select="$name" /></name>
<description><xsl:value-of select="$description" /></description>
</item>
<xsl:call-template name="multiply">
<xsl:with-param name="count" select="$count - 1" />
<xsl:with-param name="name" select="$name" />
<xsl:with-param name="description" select="$description" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Возможно, не будет работать для другого значения счета, чем 3 ...? –
Это не сработает, когда число больше 3. Вопрос также (я цитирую): «нужно добавить 2 дополнительных ребенка в конце в зависимости от счета. Если счет 3 добавить двух детей» –
«** если ** count - 3 добавить двух дочерних "... Я просто предположил, что это всего лишь пример, но, возможно, это фиксированное требование. Только оригинальный плакат знает, что правильно. –