我有一个具体的要求,我需要更改当前元素的属性值与前面的兄弟节点值。如何将属性值更改为前兄弟值
当前XML
<com:row>
<com:new>
<com:Component ExcludeInd="false">
<com:CatTypeCode>35</com:CatTypeCode>
<com:SubCatTypeCode>055508</com:SubCatTypeCode>
<com:ComCode>1000</com:ComCode>
<com:VComponentCode>nbr</com:VComponentCode>
<com:Val Value="sometext">250</com:Val>
</com:Component>
</com:new>
</com:row>
XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:com="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="../com:Component/com:Val">
<xsl:element name="com:Val" namespace="http://www.w3.org/2001/XMLSchema-instance">
<xsl:variable name="myVar" select="preceding-sibling::com:VComponentCode"/>
<xsl:attribute name="ValueType"><xsl:value-of select="$myVar"/></xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
预计XML
<com:row>
<com:new>
<com:Component ExcludeInd="false">
<com:CatTypeCode>35</com:CatTypeCode>
<com:SubCatTypeCode>055508</com:SubCatTypeCode>
<com:ComCode>1000</com:ComCode>
<com:VComponentCode>nbr</com:VComponentCode>
<com:Val Value="nbr">250</com:Val>
</com:Component>
</com:new>
</com:row>
我能够改变日当我对值进行硬编码时,属性中的e值不作为变量。
你的XML不能很好地形成张贴,一些标签不具备相应的结束标记:'COM:SubCateTypeCode' VS'COM:SubCatTypeCode','COM:ComCode' VS'COM:ComponentCode'。请修复该问题 – har07