SAP ABAP实用技巧介绍系列之 ABAP XSLT 使用attribute增加新的属性

110 阅读1分钟

Created by Jerry Wang on Jul 01, 2014

用于测试的xml:

<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>

对于匹配到的第一个node,如果其node name不为空,则输出一个新的名为pii的node,并且用xsl:attribute给该node assign一个新的attribute,name为node_value, 其value等于匹配的node name。
[外链图片转存失败(img-COxHAZPu-1562254210945)(user-images.githubusercontent.com/5669954/273…)]
输出:
[外链图片转存失败(img-qs3m2J4W-1562254210946)(user-images.githubusercontent.com/5669954/273…)]
若再line22后加上一行 <xsl:copy-of select="./*"/>,则能将输入的xml也copy到输出中:
[外链图片转存失败(img-AYcIOuAn-1562254210947)(user-images.githubusercontent.com/5669954/273…)]

将line7替换成select="node()|*"能达到同样效果。

select="@*"则匹配xml中所有的unique attribute:
[外链图片转存失败(img-HFVZTAam-1562254210949)(user-images.githubusercontent.com/5669954/273…)]
例如xml文件中catalog node 的attribute priority值为very high:
[外链图片转存失败(img-EEn8eMdh-1562254210951)(user-images.githubusercontent.com/5669954/273…)]
匹配结果:
[外链图片转存失败(img-HkqKC1GC-1562254210951)(user-images.githubusercontent.com/5669954/273…)]