[英]How to amend or add XML attribute with XSLT based on xml node sibligs value?
是否可以通過基於節點同級/子級值更改或添加屬性來修改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>
非常感謝您的指導。
將以下同級軸與以下模板一起使用:
<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']
選擇所有具有FieldName屬性值為“行索引”的節點。 應用於您的XML的XSLT產生以下結果:
<FieldMatchResult FieldName="Record_Amount" Tag="Record_1_Amount"/>
<FieldMatchResult FieldName="Record_Amount" Tag="Record_2_Amount"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.