首页 文章

如何在XPath表达式中包含换行符?

提问于
浏览
-1

有没有办法在XPath表达式中添加换行符?

这是我目前的表达方式无效

{concat(@Parameter1, $NullObj, @Parameter2)}

变量 $NullObj 等于 
 LINEFEED字符的十六进制值 .

输出为“Parameter1 Parameter2” .

编辑:不幸的是,我正在使用Xpath / Xsl 1.0 .

编辑:下面的示例代码 . XML:

<Post>
  <Title>Title Text</Title>
  <Body>Body Text </Body>
  <Author>Author Name</Author>
</Post>

示例XSL

<xsl:Variable name="NullObj">
<xsl:value-of select="'&#xa;'" />
</xsl:variable>

Edit2:完整的Xpath上下文,根据要求:

<xsl:value-of select="concat("The title is", @Title, $NullObj, "The Body reads:", @Body, $NullObj, "The Author is", @Author)" />

结果输出不包括换行符 . 我错过了什么基本的东西?我也尝试用 &#10; 替换 &#xa; ,但没有运气 . 谢谢你的耐心 .

2 回答

  • 1

    要证明Minimal, Complete, and Verifiable example的含义:

    以下样式表:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:variable name="NullObj">
        <xsl:value-of select="'&#xa;'" />
    </xsl:variable>
    
    <xsl:template match="/Post">
        <xsl:copy>
            <xsl:value-of select="concat('The title is: ', Title, $NullObj, 'The Body reads:', Body, $NullObj, 'The Author is: ', Author)" />
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    应用于您的示例输入,返回:

    <?xml version="1.0" encoding="UTF-8"?>
    <Post>The title is: Title Text
    The Body reads:Body Text 
    The Author is: Author Name</Post>
    

    如您所见,换行符是您期望的 .


    编辑:

    显然,你正试图产生 HTML output - 所以正确的答案是:

    <xsl:value-of select="concat('The title is: ', Title)" />
    
    <xsl:value-of select="concat('The Body reads:', Body)" />
    <xsl:value-of select="concat('The Author is: ', Author)" />
  • 0

    解决了它 . 问题原来是一个白色空间问题 . 显然将其设置为“pre”以外的任何内容都会忽略IE中的Xpath换行符 .

相关问题