XSLT 替换值 [英] XSLT replace value

查看:23
本文介绍了XSLT 替换值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的 XML

I have an XML like this

<?xml version="1.0" encoding="UTF-8"?>
<OMDefault xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <PrintDollarsAndCents>X</PrintDollarsAndCents>
   <MailAddrLine1>Add1</MailAddrLine1>
   <MailAddrLine2>Add2</MailAddrLine2>
</OMDefault>

我想要一个 XSLT 来将 XML 转换成这个

I would like to have an XSLT to transform the XML to this

<?xml version="1.0" encoding="UTF-8"?>
<OMDefault xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <PrintDollarsAndCents>Y</PrintDollarsAndCents>
   <MailAddrLine1>Add1</MailAddrLine1>
   <MailAddrLine2>Add2</MailAddrLine2>
 </OMDefault>

请注意,如果属性为 PrintDollarsAndCents 且其值为X",则X"将转换为Y"有人可以帮我解决这个问题吗?因为我对这个 XSLT 很陌生.

Please notice the 'X' gets transformed to 'Y' if the attribute is PrintDollarsAndCents and its value is 'X' Could someone please help me with this? As I am very new to this XSLT thing.

先谢谢你.

推荐答案

基本上你想要一个带有覆盖规则的身份转换.

Basically you want an identity transform, with override rules.

以下变换

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="PrintDollarsAndCents/text()[.='X']">Y</xsl:template>

</xsl:stylesheet>

应用于您的输入,产生结果:

applied to your input, produces the result:

<OMDefault xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <PrintDollarsAndCents>Y</PrintDollarsAndCents>
   <MailAddrLine1>Add1</MailAddrLine1>
   <MailAddrLine2>Add2</MailAddrLine2>
</OMDefault>

第一个模板是身份转换,它精确地复制输入文档.

The first template is an identity transform, which copies the input document exactly.

第二个模板覆盖值为 X 的文本节点,这些节点是 PrintDollarsAndCents 模板的子级.请注意,它发出值 Y 而不是其实际内容.

The second template overrides text nodes with a value of X that are children of a PrintDollarsAndCents template. Note that it emits the value Y instead of its actual content.

这篇关于XSLT 替换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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