我有以下 XML:
<root>
<book>
<element2 location="file.txt"/>
<element3>
<element3child/>
</element3>
</book>
<book>
<element2 location="difffile.txt"/>
</book>
</root>
我需要能够复制所有内容,但要检查我们是否在 /root/book/element2[@location='whateverfile'] 中。如果我们在这里,我们需要检查兄弟 element3 是否存在,如果不存在,我们添加一个<element3>
. 另一方面,如果它已经存在,我们需要转到它的子元素并找到last()
并附加我们自己的元素 say <element3child>
。
到目前为止,我想出了以下内容。但请记住,我是 XSLT 的新手,需要一些语法等方面的帮助。
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root/book/element2[@location='file.txt']/../*/last()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<element3child/>
</xsl:template>