如何使用xsl用xml中的另一个标签替换一个标签 [英] How to replace a tag with another tag in xml using xsl

查看:47
本文介绍了如何使用xsl用xml中的另一个标签替换一个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 xml 文件如下所示.

My xml file looks like below.

<rule>  
  <name>86</name>
  <ruleId>100</ruleId>
  <ruleVersion>1.0</ruleVersion>
  <brlVersion>1.0</brlVersion>
</rule>

我需要用 brlName 替换 name,我需要添加另一个标签作为 drlName.输出应该如下所示.

I need to replace name with brlName and i need to add another tag as drlName.The output should looks like below.

<rule>  
  <brlName>86</brlName>
  <ruleId>100</ruleId>
  <ruleVersion>1.0</ruleVersion>
  <brlVersion>1.0</brlVersion>
  <drlName>86_1.0</drlName>
</rule>

请帮助我使用相应的 xsl 以获得所需的输出.感谢您的帮助!

Please help me with corresponding xsl to get desired output. Appreciated your help!

推荐答案

这是身份转换的典型任务(下面转换中的第一个模板规则).只有两个覆盖(最后两个规则).

This is the typical task for the identity transform (the first template rule in the transform below). Just two overrides (the last two rules).

XSLT 1.0Saxon 6.5.5

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="name">
        <brlName><xsl:value-of select="."/></brlName>
    </xsl:template>

    <xsl:template match="brlVersion">
        <xsl:copy-of select="."/>
        <drlName><xsl:value-of select="preceding-sibling::name"/>_1.0</drlName>
    </xsl:template>

</xsl:stylesheet>

这篇关于如何使用xsl用xml中的另一个标签替换一个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆