0
在我的源XML中,我將重新分配從數字1開始的所有實例ID(直到存在)。我正在更新配置文件元素下的Apple,Mango,Banana InstanceId的以上值。在這裏,我忽略配置文件InstanceID級別的發生。我的要求是,只有當在一個給定的「個人資料」至少一個實例ID大於25根據特定屬性值更新XML屬性
<Root>
<Properties>
<Props></Props>
<Input></Input>
<Profile InstanceID="4" ObjectID="XYZ">
<foo>Profile 1</foo><!-- Need to update these set as atleast one instanceID is greater than 25 -->
<Apple InstanceID="26" ObjectID="ABC" Type="103"></Apple>
<Mango InstanceID="1" ObjectID="DEF" Type="103"></Mango>
<Mango InstanceID="27" ObjectID="GHI" Type="103"></Mango>
<Banana InstanceID="29" ObjectID="GHI1" Type="103"></Banana>
</Profile>
</Properties>
<Properties>
<Props></Props>
<Input></Input>
<Profile InstanceID="4" ObjectID="XYZ">
<foo>Profile 1</foo><!-- no need to update these set as no instanceID is greater than 25 -->
<Apple InstanceID="21" ObjectID="MNO" Type="103"></Apple>
<Mango InstanceID="21" ObjectID="PQR" Type="103"></Mango>
<Mango InstanceID="23" ObjectID="EFG" Type="103"></Mango>
<Mango InstanceID="24" ObjectID="EFG123" Type="103"></Mango>
<Banana InstanceID="25" ObjectID="GHI1" Type="103"></Banana>
</Profile>
</Properties>
</Root>
下面是我用
# XSLT #
<?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 omit-xml-declaration="yes" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Profile/*/@InstanceID">
<xsl:attribute name="InstanceID">
<xsl:value-of select="count(../preceding-sibling::*[@InstanceID]) + 1" />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
而這些的XSLT來觸發這些XSLT代碼輸出/轉換的XML
# Transformed XML #
<Root>
<Properties>
<Props></Props>
<Input></Input>
<Profile InstanceID="4" ObjectID="XYZ"><!-- no need to update these instanceID -->
<foo>Profile 1</foo>
<Apple InstanceID="1" ObjectID="ABC" Type="103"></Apple>
<Mango InstanceID="2" ObjectID="DEF" Type="103"></Mango>
<Mango InstanceID="3" ObjectID="GHI" Type="103"></Mango>
<Banana InstanceID="4" ObjectID="GHI1" Type="103"></Banana>
</Profile>
</Properties>
<Properties>
<Props></Props>
<Input></Input>
<Profile InstanceID="4" ObjectID="XYZ"><!-- no need to update these instanceID -->
<foo>Profile 1</foo>
<Apple InstanceID="1" ObjectID="MNO" Type="103"></Apple>
<Mango InstanceID="2" ObjectID="PQR" Type="103"></Mango>
<Mango InstanceID="3" ObjectID="EFG" Type="103"></Mango>
<Mango InstanceID="4" ObjectID="EFG123" Type="103"></Mango>
<Banana InstanceID="5" ObjectID="GHI1" Type="103"></Banana>
</Profile>
</Properties>
</Root>