简体   繁体   English

XML使用XSLT 2.0将Node转换为String

[英]XML convert Node into String using XSLT 2.0

I've searched for renaming an XML node to a string. 我搜索了将XML节点重命名为字符串。 I have found examples of strings within XML to Nodes but not the other way. 我找到了XML到Nodes中的字符串示例,但没有找到其他示例。

Is it possible to do the following 是否可以执行以下操作

<par>
<run> Some text <break/> </run>
</par>

<par>
<run> some text with no carraige return</run>
</par>

To: 至:

<par>
<run> Some text &#10; </run>
</par>

Many thanks in advance for any replies. 非常感谢您的任何答复。

Dono 堂野

Certainly that's possible. 当然可以。 Just use an identity transform and handle <break> specially. 只需使用身份转换并专门处理<break> Instead of copying it using <xsl:copy> , output any text you like: 不用使用<xsl:copy>复制它,而是输出您喜欢的任何文本:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="break">
    <xsl:value-of select="'&#10;'"/>
  </xsl:template>

</xsl:stylesheet>

I you want the literal output &#10; 我想要文字输出&#10; you can use disable-output-escaping , like 您可以使用disable-output-escaping ,例如

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="break">
    <xsl:value-of select="'&amp;#10;'" disable-output-escaping="yes"/>
  </xsl:template>

</xsl:stylesheet>

However, this is an optional feature and not guaranteed to be supported by any XSLT processor. 但是,这是一项可选功能,不能保证任何XSLT处理器都支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM