使用XSLT检查特定属性的值并更新文本

时间:2015-10-20 18:31:13

标签: xml xslt

输入xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<advert>
        <adKey>20151014001</adKey>
        <adStatus>REPEAT</adStatus>
        <adLabelInformation/>
        <formatInformation shape="Horizontal">1/2 TAB</formatInformation> 
        <adPositioningInformation/>
</advert>

预期的XML:

<?xml version="1.0" encoding="UTF-8"?>
<advert>
            <adKey>20151014001</adKey>
            <adStatus>REPEAT</adStatus>
            <adLabelInformation/>
            <formatInformation shape="Tab_H">RHP1/2</formatInformation> 
            <adPositioningInformation/>
</advert>

检查formatInformation中的text和shape属性,如果formatInformation有text&#34; 1/2 TAB&#34;并具有形状属性&#34;水平&#34;将形状属性值更改为&#34; Tab_H&#34;和文字为&#34; RHP1 / 2&#34;使用XSLT

1 个答案:

答案 0 :(得分:1)

希望这可以帮助你:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:template match="advert">
         <xsl:choose>
             <xsl:when test="./formatInformation[@shape='Horizontal']='1/2 TAB'">
                 <advert>
                     <adKey><xsl:value-of select="./adKey"/></adKey>
                     <adStatus><xsl:value-of select="./adStatus"/></adStatus>
                     <adLabelInformation><xsl:value-of select="./adLabelInformation"/></adLabelInformation>
                     <formatInformation shape="Tab_H"><xsl:value-of select="'RHP1/2'"/></formatInformation>
                     <adPositioningInformation><xsl:value-of select="./adPositioningInformation"/></adPositioningInformation>
                 </advert>
             </xsl:when>
             <xsl:otherwise>
                 <xsl:copy-of select="."/>
             </xsl:otherwise>
         </xsl:choose>
    </xsl:template>

</xsl:stylesheet>