如何在XSLT中复制节点内容并更新标签,转换toaEntry为auth.ref并给charfill加属性
XSLT转换实现方案
你需要的转换可以基于XSLT恒等模板扩展实现,完整代码如下:
<?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> <!-- 匹配根节点auth,转换为sample --> <xsl:template match="auth"> <sample> <xsl:apply-templates select="@*|node()"/> </sample> </xsl:template> <!-- 匹配toaEntry节点,转换为auth.ref --> <xsl:template match="toaEntry"> <auth.ref> <xsl:apply-templates select="@*|node()"/> </auth.ref> </xsl:template> <!-- 匹配charfill节点,添加numchar='2'属性 --> <xsl:template match="charfill"> <charfill numchar="2"> <xsl:apply-templates select="@*|node()"/> </charfill> </xsl:template> </xsl:stylesheet>
代码逻辑说明
- 恒等模板作为基础规则,所有未指定匹配规则的内容都会原样保留,不需要手动处理
ital、reference等其他标签的复制逻辑 - 单独指定
auth、toaEntry、charfill三个节点的转换规则,分别完成根节点重命名、条目节点重命名、填充标签加属性的需求 - 直接对输入XML执行上述XSLT转换,即可得到你需要的输出结果。
内容的提问来源于stack exchange,提问作者Anuj Parmar
相关产品推荐
相关产品推荐

