给定一个输入XML,如下所示:
<foodGroup>
<fruit>
<label>Apple</label>
<value>1</value>
</fruit>
<fruit>
<label>Banana</label>
<value>2</value>
</fruit>
<vegetable/>
</foodGroup>
XSL转换是什么,它将检查label ='Orange'和value = non 0的水果是否存在。如果它缺失,则将水果/标签和水果/价值结构添加到输出中。
这样的事情:
<foodGroup>
<fruit>
<label>Apple</label>
<value>1</value>
</fruit>
<fruit>
<label>Banana</label>
<value>2</value>
</fruit>
<fruit>
<label>Orange</label>
<value>3</value>
</fruit>
<vegetable />
</foodGroup>
答案 0 :(得分:0)
相关测试为<xsl:if test="not(fruit[label = 'Orange' and value != 0])">
。
以下XSLT给出了您要求的结果:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="foodGroup">
<xsl:copy>
<xsl:apply-templates />
<xsl:if test="not(fruit[label = 'Orange' and value != 0])">
<fruit>
<label>Orange</label>
<value><xsl:value-of select="count(fruit) + 1"/></value>
</fruit>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这假定您希望水果值每次增加1。如果这不是一个正确的假设,请指定您希望添加橙色的值。