XSLT:更改节点内部文本 [英] XSLT: change node inner text

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

问题描述

我需要转换以下 xml 文档:

I need to transform the following xml doc:

<a>
  <b/>
  <c/>
   myText
</a>

进入这个:

<a>
  <b/>
  <c/>
   differentText
</a>

所以,我写了这个 XSLT 文档

So, i wrote this XSLT document

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" version="1.0" omit-xml-declaration="no" />

  <xsl:template match="/a/text()">
    <a>
      <b/>
      <c/>
      differentText
    </a>
</xsl:template>
</xsl:stylesheet>

这样,我得到以下结果:

This way, i get the following result:

<?xml version="1.0" encoding="utf-8"?>
<a>
  <b /><c />
  differentText
</a>
<a>
  <b /><c />
  differentText
</a>
<a>
  <b /><c />
  differentText
</a>

结果出现了 3 次重复,因为正在进行 3 场比赛.为什么?我能修好吗?谢谢

The result appears repeated 3 times because 3 matches are being done.. Why? I could i fix it? Thanks

推荐答案

排除纯空白文本节点.了解并使用 说明.

这种转变:

<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="a/text()">
   <xsl:text>Diferent text</xsl:text>
 </xsl:template>
</xsl:stylesheet>

当应用于提供的 XML 文档时,产生想要的正确结果.

在特定模板的匹配表达式中不需要复杂的谓词

我们应该努力实现最简单、最短、最优雅、最易读、最易理解的解决方案,以充分利用语言的全部力量.

We should be striving for the simplest, shortest, most elegant, most readable, most understandable solution that employs the full power of the language.

这样的解决方案很可能最容易理解、最容易实现并且最有可能被任何 XSLT 处理器优化,从而实现最高效的实现.

Chances are that such a solution will be most understood, most easy to implement and most likely to be optimized by any XSLT processor, resulting in a most efficient implementation.

这篇关于XSLT:更改节点内部文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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