简体   繁体   中英

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' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM