-1

Is there a way to add a line break to an XPath expression?

Here is my current expression which isn't working

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

Where the variable $NullObj is equal to 
 the hex value of the LINEFEED character.

This outputs as "Parameter1 Parameter2."

EDIT: Unfortunately, I'm using Xpath / Xsl 1.0.

EDIT: Sample code below. XML:

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

Sample XSL

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

Edit2: Full Xpath context, as requested:

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

The resulting output does not include line breaks. Am I missing something fundamental? I've also tried replacing &#xa; with &#10;, but no luck. Thanks for your patience.

Rob Ot
  • 11
  • 1
  • 6
  • 1
    Possible duplicate of [Line breaks (\n) via concat() in XPath?](http://stackoverflow.com/questions/36312794/line-breaks-n-via-concat-in-xpath) – kjhughes May 04 '16 at 16:54
  • 1
    Please post a **reproducible** example. – michael.hor257k May 04 '16 at 17:09
  • thanks @kjhughes. I tried using this solution and it didn't work. It might be because the verion of XPath + XSL I'm using are not 2.0. I should have clarified that in my original post. – Rob Ot May 04 '16 at 17:24
  • @michael.hor257k, added. Thanks in advance for any help you can provide. – Rob Ot May 04 '16 at 17:40
  • That's still not reproducible. Please show the XPath expression in its full XSLT context. See: [mcve] – michael.hor257k May 04 '16 at 17:42
  • @michael.hor257k Sorry about that. I've added the full tag. – Rob Ot May 04 '16 at 18:42
  • 1
    I don't think you understand what "complete" means. It means I should be able to copy/paste your code and run it *as is* without having to add anything. -- Also, the code makes no sense: `Title`, `Body` and `Author` are not attributes. -- Note also that XML is case-sensitive: `` would produce an error. -- And your expression is invalid on account of nested quotes. – michael.hor257k May 04 '16 at 18:53
  • May be you are using a MAC? Than the new line is a ` ` – hr_117 May 04 '16 at 18:59

2 Answers2

1

To demonstrate what is meant by Minimal, Complete, and Verifiable example:

The following stylesheet:

<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>

applied to your example input, returns:

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

As you can see, the line breaks are where you expect them to be.


Edit:

Apparently, you are trying to produce HTML output - so the correct answer would be:

<xsl:value-of select="concat('The title is: ', Title)" />
<br/>
<xsl:value-of select="concat('The Body reads:', Body)" />
<br/>
<xsl:value-of select="concat('The Author is: ', Author)" />
Community
  • 1
  • 1
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

Solved it. The problem turned out to be a white-space issue. Apparently setting it to anything other than "pre" would ignore the Xpath line breaks in IE.

Rob Ot
  • 11
  • 1
  • 6
  • 1
    Well, if you are producing HTML to be viewed in a web browser - **and how are we supposed to know that from the snippets you have posted?!!** - then you really should be producing **HTML line breaks** i.e. `
    `. https://developer.mozilla.org/en/docs/Web/HTML/Element/br Then you won't need to resort to using the `
    ` tag which causes a number of other effects you probably don't want (such as monospaced font).
    – michael.hor257k May 05 '16 at 15:06