xslt - xslt2.0: identity transformation with conditional change
问题描述
Suppose the following xml
<map>
<entry>
<string>KEY1</string>
<int>value</int>
</entry>
<entry>
<string>KEY2</string>
<string>value</string>
</entry>
<entry>
<string>KEY3</string>
<map>
...
</map>
</entry>
<entry>
<string>KEY4</string>
<string>special_value</string>
</entry>
</map>
Each map contains several entries. Each entry has a KEY (always of type string, always the first element) and a value of a certain type (string, int or even a nested map).
I want to have an identity transformation (xsl2.0) with the following exception: Whenever there is a map containing entry key="KEY4" with string-value="special_value", perform a "fn:replace" on the string-value of the entry with key="KEY2" in the same map. The order of the entries is not known.
It can be assumend that KEY2 and KEY4 are always of type string. It would help if the check for "special_value" could be case insensitive and maybe a regular expression
解决方案
This seems rather trivial: in addition to the identity transform template, use a template like:
<xsl:template match="entry[string[1]='KEY2'][../entry[string[1]='KEY4' and string[2]='special_value']]/string[2]">
<xsl:copy>
<xsl:value-of select="replace(., 'x', 'y')"/>
</xsl:copy>
</xsl:template>
This tests for the existence of a sibling KEY4
entry with a special_value
value. To make it case-insensitive, change:
string[2]='special_value'
to:
lower-case(string[2])='special_value'
推荐阅读
- amazon-web-services - Cloudfront 使用签名 URL 获取 S3 对象的间歇性 403 CORS 错误(访问控制允许来源)
- r - 饼图 - 如何在正确的位置获取百分比文本?
- python - 一行,三个变量
- xcode - 为什么标题栏与视图控制器内容重叠?
- xcode - 收到与情节提要相关的构建错误
- node.js - Ajax 使用参数调用 API
- html - 没有 form.as_p 的 Django 自定义模板
- transform - Vega-Lite 上的微分变换
- python - Python中的反斜杠和引号转义序列有问题吗?
- python - 熊猫:将数据附加到熊猫数据框中的问题