输入XML:
.....
..... <!-- Many lines of code -->
.....
<Attribute Name="Attr1">
<Array>
<Object Type="Object1">
<Attribute Name="Attr2">
"123"
"234"
</Attribute>
<Attribute Name="Attr3">"456"</Attribute>
</Object>
<Object Type="Object2">
<Attribute Name="Attr4">
"444"
"555"
</Attribute>
</Object>
<Object Type="Object3">
<Attribute Name="Attr5">
"666"
"777"
"888"
<!-- My new item should come here -->
</Attribute>
</Object>
</Array>
</Attribute>
我尝试了以下XSLT将新条目(999)添加到上述位置“我的新项目应该来到这里”。
在插入此新项目之前,我想检查节点Attribute(<Attribute Name = "Attr5")
是否具有值“888”。只有它包含“888”时,我才应该插入“999”。你能告诉我这是如何实现的吗?
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="obj" select='"Object3"'/>
<xsl:variable name="attr" select='"Attr5"'/>
<xsl:param name="evalue">"999"</xsl:param>
<xsl:template match="Attribute/Array/Object[@Type=$obj]/Attribute[@Name=$attr]">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:value-of select="." />
<xsl:value-of select="$evalue"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出XML
<Attribute Name="Attr1">
<Array>
<Object Type="Object1">
<Attribute Name="Attr2">
"123"
"234"
</Attribute>
<Attribute Name="Attr3">"456"</Attribute>
</Object>
<Object Type="Object2">
<Attribute Name="Attr4">
"444"
"555"
</Attribute>
</Object>
<Object Type="Object3">
<Attribute Name="Attr5">
"666"
"777"
"888"
"999"
</Attribute>
</Object>
</Array>
</Attribute>
这是最终XML的样子。请帮帮我
答案 0 :(得分:2)
您的模板中缺少许多说明......
<xsl:template match="Attribute/Array/Object[@Type=$obj]/Attribute[@Name=$attr and contains(., '"888"')]">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:value-of select="." />
<xsl:value-of select="$evalue"/>
</xsl:copy>
</xsl:template>