有没有办法通过根据节点兄弟/子值更改或添加属性来修改XML?
我需要改造:
<FieldMatchResult FieldName="Record_Amount">
进入以下之一:
<FieldMatchResult FieldName="Record_1_Amount">
或
<FieldMatchResult FieldName="Record_Amount" Tag="Record_1_Amount">
这是我的样本,我需要从“行索引”元素中提取值
<?xml version="1.0"?>
<ArtifactMatchResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<SubArtifacts>
<ArtifactMatchResult ArtifactName="Data Record">
<Fields>
<FieldMatchResult FieldName="Record_Amount">
<Values>
<anyType xsi:type="xsd:double">123456.5</anyType>
</Values>
</FieldMatchResult>
<FieldMatchResult FieldName="Record_Rate" >
<Values>
<anyType xsi:type="xsd:double">1.25</anyType>
</Values>
</FieldMatchResult>
<FieldMatchResult FieldName="Row Index">
<Values>
<anyType xsi:type="xsd:double">1</anyType>
</Values>
</FieldMatchResult>
</Fields>
<SubArtifacts />
</ArtifactMatchResult>
<ArtifactMatchResult ArtifactName="Data Record">
<Fields>
<FieldMatchResult FieldName="Record_Amount">
<Values>
<anyType xsi:type="xsd:double">123456.5</anyType>
</Values>
</FieldMatchResult>
<FieldMatchResult FieldName="Record_Rate" >
<Values>
<anyType xsi:type="xsd:double">1.25</anyType>
</Values>
</FieldMatchResult>
<FieldMatchResult FieldName="Row Index">
<Values>
<anyType xsi:type="xsd:double">2</anyType>
</Values>
</FieldMatchResult>
</Fields>
<SubArtifacts />
</ArtifactMatchResult>
</SubArtifacts>
</ArtifactMatchResult>
非常感谢任何指示。
答案 0 :(得分:2)
将以下兄弟axis与以下模板一起使用:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="XML" omit-xml-declaration="yes"/>
<xsl:template match="ArtifactMatchResult/SubArtifacts/ArtifactMatchResult/Fields">
<FieldMatchResult FieldName="Record_Amount">
<xsl:attribute name="Tag">
<xsl:value-of select="concat('Record_', */following-sibling::FieldMatchResult[@FieldName = 'Row Index']/Values/anyType, '_Amount')" />
</xsl:attribute>
</FieldMatchResult>
</xsl:template>
</xsl:stylesheet>
concat()
连接两个或多个以逗号分隔的字符串。*
选择上下文节点的所有子节点。following-sibling::FieldMatchResult
选择跟随上下文节点子节点的所有FieldMatchResult
兄弟节点。[@FieldName = 'Row Index']
选择具有值&#39;行索引&#39;。这个应用于XML的XSLT给出了以下结果:
<FieldMatchResult FieldName="Record_Amount" Tag="Record_1_Amount"/>
<FieldMatchResult FieldName="Record_Amount" Tag="Record_2_Amount"/>