Producing a new line in XSLT

2019-01-03 12:34发布

I want to produce a newline for text output in XSLT. Any ideas?

标签: xslt
12条回答
孤傲高冷的网名
2楼-- · 2019-01-03 13:14

My favoured method for doing this looks something like:

<xsl:stylesheet>

<xsl:output method='text'/>

<xsl:variable name='newline'><xsl:text>
</xsl:text></xsl:variable>

<!-- note that the layout there is deliberate -->

...

</xsl:stylesheet>

Then, whenever you want to output a newline (perhaps in csv) you can output something like:

<xsl:value-of select="concat(elem1,elem2,elem3,$newline") />

I've used this technique when outputting sql from xml input. In fact, I tend to create variables for commas, quotes and newlines.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-03 13:16

The following XSL code will produce a newline (line feed) character:

<xsl:text>&#xa;</xsl:text>

For a carriage return, use:

<xsl:text>&#xd;</xsl:text>
查看更多
一纸荒年 Trace。
4楼-- · 2019-01-03 13:18

I've noticed from my experience that producing a new line INSIDE a <xsl:variable> clause doesn't work. I was trying to do something like:

<xsl:variable name="myVar">
  <xsl:choose>
    <xsl:when test="@myValue != ''">
      <xsl:text>My value: </xsl:text>
      <xsl:value-of select="@myValue" />
      <xsl:text></xsl:text> <!--NEW LINE-->
      <xsl:text>My other value: </xsl:text>
      <xsl:value-of select="@myOtherValue" />
    </xsl:when>
  </xsl:choose>
<xsl:variable>

<div>
  <xsl:value-of select="$myVar"/>
</div>

Anything I tried to put in that "new line" (the empty <xsl:text> node) just didn't work (including most of the simpler suggestions in this page), not to mention the fact that HTML just won't work there, so eventually I had to split it to 2 variables, call them outside the <xsl:variable> scope and put a simple <br/> between them, i.e:

<xsl:variable name="myVar1">
  <xsl:choose>
    <xsl:when test="@myValue != ''">
      <xsl:text>My value: </xsl:text>
      <xsl:value-of select="@myValue" />
    </xsl:when>
  </xsl:choose>
<xsl:variable>

<xsl:variable name="myVar2">
  <xsl:choose>
    <xsl:when test="@myValue != ''">
      <xsl:text>My other value: </xsl:text>
      <xsl:value-of select="@myOtherValue" />
    </xsl:when>
  </xsl:choose>
<xsl:variable>

<div>
  <xsl:value-of select="$myVar1"/>
  <br/>
  <xsl:value-of select="$myVar2"/>
</div>

Yeah, I know, it's not the most sophisticated solution but it works, just sharing my frustration experience with XSLs ;)

查看更多
姐就是有狂的资本
5楼-- · 2019-01-03 13:18

I second Nic Gibson's method, this was always my favorite:

<xsl:variable name='nl'><xsl:text>
</xsl:text></xsl:variable>

However I have been using the Ant task <echoxml> to create stylesheets and run them against files. The task will do attribute value templates, e.g. ${DSTAMP} , but is also will reformat your xml, so in some cases, the entity reference is preferable.

<xsl:variable name='nl'><xsl:text>&#xa;</xsl:text></xsl:variable>
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-03 13:20

I couldn't just use the <xsl:text>&#xa;</xsl:text> approach because if I format the XML file using XSLT the entity will disappear. So I had to use a slightly more round about approach using variables

<xsl:variable name="nl" select="'&#10;'"/>
<xsl:template match="/">
    <xsl:value-of select="$nl" disable-output-escaping="no"/>
    <xsl:apply-templates select="*"/>
</xsl:template>
查看更多
倾城 Initia
7楼-- · 2019-01-03 13:21

You can try,

<xsl:text>&#xA;</xsl:text>

It will work.

查看更多
登录 后发表回答