我有这样的XML
<?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>
Run Code Online (Sandbox Code Playgroud)
我想有一个XSLT将XML转换为此
<?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>
Run Code Online (Sandbox Code Playgroud)
请注意,如果属性为PrintDollarsAndCents并且其值为"X",则'X'将转换为'Y'.有人可以帮助我吗?因为我对这个XSLT事物很新.
先感谢您.
har*_*rpo 20
基本上,您需要使用覆盖规则进行身份转换.
以下转换
<?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>
Run Code Online (Sandbox Code Playgroud)
应用于您的输入,生成结果:
<OMDefault xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PrintDollarsAndCents>Y</PrintDollarsAndCents>
<MailAddrLine1>Add1</MailAddrLine1>
<MailAddrLine2>Add2</MailAddrLine2>
</OMDefault>
Run Code Online (Sandbox Code Playgroud)
第一个模板是身份转换,它完全复制输入文档.
与值的第二模板覆盖文本节点X是一个儿童PrintDollarsAndCents模板.请注意,它会发出值Y而不是实际内容.