以下是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<consentException fpmlVersion="5-6" >
<header>
<messageId messageIdScheme="www.test.com">LH_msf_id</messageId>
<inReplyTo messageIdScheme="">2424234243</inReplyTo><!-- message Id of entering party -->
<sentBy>test</sentBy>
<sendTo>SEF1</sendTo>
<creationTimestamp>2010-09-09T10:00:00-00:00</creationTimestamp>
</header>
<correlationId correlationIdScheme="">SEF_correlationId</correlationId>
<reason>
<reasonCode>3500001</reasonCode>
<description>Invalid Currency ABC</description>
</reason>
<reason>
<reasonCode>3500043</reasonCode>
<description>Organization XYZ is not defined</description>
</reason>
</consentException>以下是我的XSLT文件:
<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="consentException/header/sentBy">
<Hello><xsl:apply-templates select="@*|node()"/></Hello>
</xsl:template>
</xsl:stylesheet>现在,XSLT代码替换了xml文件中的标记:( Hello标记)
<?xml version="1.0" encoding="UTF-8"?><consentException fpmlVersion="5-6">
<header>
<messageId messageIdScheme="www.test.com/">LH_msf_id</messageId>
<inReplyTo messageIdScheme="">2424234243</inReplyTo><!-- message Id of entering party -->
<Hello>Test</Hello>
<sendTo>SEF1</sendTo>
<creationTimestamp>2010-09-09T10:00:00-00:00</creationTimestamp>
</header>
<correlationId correlationIdScheme="">SEF_correlationId</correlationId>
<reason>
<reasonCode>3500001</reasonCode>
<description>Invalid Currency ABC</description>
</reason>
<reason>
<reasonCode>3500043</reasonCode>
<description>Organization XYZ is not defined</description>
</reason>
</consentException>但是,如果我希望您替换XML文件中的所有标记,而不是指定每个标记,那么有更好的方法吗?
我不确定这一点,但是如果有一个包含XML标记的文件,以及需要替换哪些XML标记,那么它能以某种方式使用XSLT代码引用该文件吗?
我正在努力寻找最合适的方法来完成这项任务。
注意:标记替换只能用XSLT代码完成。
更新
下面是最后的XML应该是什么样子:
<?xml version="1.0" encoding="utf-8"?>
<consentException fpmlVersion="5-6" >
<header>
<FPHdMsgID FPHdMsgIDScheme="www.test.com">LH_msf_id</FPHdMsgID>
<inReplyTo messageIdScheme="">2424234243</inReplyTo><!-- message Id of entering party -->
<FPHdSentBy>Traiana</FPHdSentBy>
<FPHdSentTo>SEF1</FPHdSentTo>
<FPHdCreateTime>2010-09-09T10:00:00-00:00</FPHdCreateTime>
</header>
<FPCorID FPCorIDSch="">SEF_correlationId</FPCorID>
<reason>
<FPRsnCode>3500001</FPRsnCode>
<FPRsnDesc>Invalid Currency ABC</FPRsnDesc>
</reason>
<reason>
<FPRsnCode>3500043</FPRsnCode>
<description>Organization XYZ is not defined</description>
</reason>
</consentException>发布于 2015-03-11 11:15:37
如果有一个包含XML标记的文件,以及需要替换哪些XML标记,那么它能以某种方式使用XSLT代码引用该文件吗?
是的,但是它肯定会而不是是“完成此任务的最佳方法”,因为它将引入不必要的(而且代价相当高)的步骤。如果您必须不时地更改重命名映射,并且不希望在此过程中修改主XSLT文档,这可能是一种很好的方法。否则,最好有一个模板为每个重命名。XSLT自然是冗长的,要习惯它。
发布于 2015-03-11 15:42:05
定义映射文件如下:
renaming.xml
<rename>
<rename old="foo" new="bar"/>
<rename old="fox" new="box"/>
</rename>然后编写一个“元样式表”,如下所示:
<xsl:namespace-alias stylesheet-prefix='a' result-prefix="xsl"/>
<xsl:template match="rename">
<a:template match="{@old}">
<a:element name="{@new}">
<a:copy-of select="@*"/>
<a:apply-templates/>
</a:element>
</a:template>
</xsl:template>针对rename.xml运行这个元样式表,生成一个XSLT样式表,每个元素名称都有一个模板规则,可以针对任何源文档运行,并执行重命名。
当然,我忽略了一些细节,比如处理名字不在重命名文件中的元素,或者处理名称空间。但任务很简单。
https://stackoverflow.com/questions/28984442
复制相似问题