XSLT:如何将 XML 节点转换为字符串 [英] XSLT: How to convert XML Node to String

查看:76
本文介绍了XSLT:如何将 XML 节点转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<ROOT>
   <A>
      <B>TESTING</B>
   </A>
</ROOT>

XSL:

<xsl:variable name="nodestring" select="//A"/>
<xsl:value-of select="$nodestring"/>

我正在尝试使用 XSL 将 XML 节点集转换为字符串.有什么想法吗?

I am trying to convert XML nodeset to string using XSL. Any thoughts?

推荐答案

您需要序列化节点.你的例子最简单的是

You need to serialize the nodes. The most simple for your example would be something like

<xsl:template match="ROOT">
  <xsl:variable name="nodestring">
    <xsl:apply-templates select="//A" mode="serialize"/>
  </xsl:variable>
  <xsl:value-of select="$nodestring"/>  
</xsl:template>

<xsl:template match="*" mode="serialize">
  <xsl:text>&lt;</xsl:text>
  <xsl:value-of select="name()"/>
  <xsl:text>&gt;</xsl:text>
  <xsl:apply-templates mode="serialize"/>
  <xsl:text>&lt;/</xsl:text>
  <xsl:value-of select="name()"/>
  <xsl:text>&gt;</xsl:text>
</xsl:template>

<xsl:template match="text()" mode="serialize">
  <xsl:value-of select="."/>
</xsl:template>

上述序列化器模板不处理例如属性、命名空间或文本节点中的保留字符,但概念应该清楚.XSLT 过程适用于节点树,如果您需要访问标签",则需要序列化节点.

The above serializer templates do not handle e.g. attributes, namespaces, or reserved characters in text nodes, but the concept should be clear. XSLT process works on a node tree and if you need to have access to "tags", you need to serialize the nodes.

这篇关于XSLT:如何将 XML 节点转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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