如何检测 XSLT 中的换行符 [英] How to detect line breaks in XSLT

查看:39
本文介绍了如何检测 XSLT 中的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够输出由换行符分隔的 XML 文档的文本.换句话说,XML:

I need to be able to output the text of an XML document separated by line breaks. In other words, the XML:

<programlisting>
public static void main(String[] args){
    System.out.println("Be happy!");  
    System.out.println("And now we add annotations.");  
}
</programlisting>

需要表示为:

<para>public static void main(String[] args){</para>
<para>    System.out.println("Be happy!"); </para>
<para>    System.out.println("And now we add annotations.");  </para>
<para>}</para>

我认为我应该能够使用 substring-before(., '\n') 但由于某种原因它无法识别换行符.

I thought that I should be able to use substring-before(., '\n') but for some reason it's not recognizing the line break.

我还尝试将每一行作为 CDATA 部分输出,以便我可以分别提取它们,但遇到了这样一个事实,即它们都混在一起形成了一个文本节点.

I also tried to output each line as a CDATA section so that I could pull those separately, but ran into the fact that they're all smushed together into a single text node.

我只是在这里使用常规 Java 进行转换.关于如何实现这一点的任何想法?

I'm just using regular Java here for transformation. Any ideas on how to accomplish this?

谢谢...

推荐答案

I.XSLT 2.0 解决方案:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="text()">
  <xsl:for-each select="tokenize(., '\n\r?')[.]">
   <para><xsl:sequence select="."></xsl:sequence></para>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<programlisting>
public static void main(String[] args){
    System.out.println("Be happy!");
    System.out.println("And now we add annotations.");
}
</programlisting>

产生了想要的、正确的结果:

<programlisting>
   <para>public static void main(String[] args){</para>
   <para>    System.out.println("Be happy!");</para>
   <para>    System.out.println("And now we add annotations.");</para>
   <para>}</para>
</programlisting>

<小时>

二.XSLT 1.0 解决方案,使用 str-split-to-words 模板 FXSLa>:


II. XSLT 1.0 solution, using the str-split-to-words template of FXSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
  <xsl:import href="strSplit-to-Words.xsl"/>
  <xsl:output indent="yes" omit-xml-declaration="yes"/>

   <xsl:strip-space elements="*"/>
   <xsl:output indent="yes" omit-xml-declaration="yes"/>

   <xsl:param name="pDelims" select="'&#xA;&#xD;'"/>

    <xsl:template match="/">
      <xsl:variable name="vwordNodes">
        <xsl:call-template name="str-split-to-words">
          <xsl:with-param name="pStr" select="/"/>
          <xsl:with-param name="pDelimiters"
                          select="$pDelims"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:apply-templates select=
      "ext:node-set($vwordNodes)/*[normalize-space()]"/>
    </xsl:template>

    <xsl:template match="word">
      <para><xsl:value-of select="."/></para>
    </xsl:template>
</xsl:stylesheet>

在同一个 XML 文档(上图)上应用此转换时,会产生相同的正确结果:

<para>public static void main(String[] args){</para>
<para>    System.out.println("Be happy!");</para>
<para>    System.out.println("And now we add annotations.");</para>
<para>}</para>

这篇关于如何检测 XSLT 中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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