基于特定属性值更新XML属性
在我的源XML中,我将所有实例ID从数字1重新分配到数字1(直到存在)。我正在更新苹果,芒果,香蕉InstanceId的上述值下的配置文件元素。这里我忽略了Profile InstanceID级别的引用。我的要求是,只有当给定的“概要文件”集中至少有一个实例id大于25时,才会触发这些xslt代码基于特定属性值更新XML属性,xml,xslt,Xml,Xslt,在我的源XML中,我将所有实例ID从数字1重新分配到数字1(直到存在)。我正在更新苹果,芒果,香蕉InstanceId的上述值下的配置文件元素。这里我忽略了Profile InstanceID级别的引用。我的要求是,只有当给定的“概要文件”集中至少有一个实例id大于25时,才会触发这些xslt代码 <Root> <Properties> <Props></Props> <Input></Input>
<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>
概况1
概况1
下面是我使用的XSLT
# 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#
这些是输出/转换的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>
#转换的XML#
概况1
概况1
我的要求是,仅当给定的“概要文件”集中至少有一个实例id大于25时才触发这些xslt代码
您只需将您的概要文件/*/@InstanceID
模板稍微具体一点。当它应用于所有Profile
元素的子元素上的InstanceID
属性时,您需要在Profile
级别添加一个谓词,以将其仅限于某些概要文件:
<xsl:template match="Profile[*/@InstanceID > 25]/*/@InstanceID">
这是因为,如果集合中的任何节点满足所需条件,则其中一侧为节点集的比较运算符为true,因此在这种情况下,如果任何子元素的InstanceID
大于25,则为true
<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>