Question

There are plenty of questions refering to ignoring whitespace in source XML data when using XSLT, but what I would really like, is to include whitespace in an XSLT template (for readability), but not having it output. Writing the template with whitespace and then transforming it to a new template without the whitespace complicates matters so much, I would like an alternative to that. As a clarification, when I have XML snippet

<CAInfo prefix="073" serial="12345675" orig="AMS" dest="BRU" issuedate="2012-12-20" currency="EUR">
    <RevisedWeight code="K" amt="140.5"/>
    <OriginalWeight code="K" amt="2"/>
</CAInfo>

and I want to output

PRE/114-12345675AMSBRU/K140.5/K2/2012-12-20/EUR

I am now using

<xsl:template match="CAInfo">
PRE/<xsl:value-of select="@prefix"/>-<xsl:value-of select="@serial"/><xsl:value-of select="@orig"/><xsl:value-of select="@dest"/>/<xsl:value-of select="RevisedWeight/@code"/><xsl:value-of select="RevisedWeight/@amt"/>/<xsl:value-of select="OriginalWeight/@code"/><xsl:value-of select="OriginalWeight/@amt"/>/<xsl:value-of select="@issuedate"/>/<xsl:value-of select="@currency"/>
</xsl:template>

What I'd much rather use is

<xsl:template match="CAInfo">
PRE/<xsl:value-of select="@prefix"/>-<xsl:value-of select="@serial"/>
<xsl:value-of select="@orig"/><xsl:value-of select="@dest"/>
/<xsl:value-of select="RevisedWeight/@code"/><xsl:value-of select="RevisedWeight/@amt"/>
/<xsl:value-of select="OriginalWeight/@code"/><xsl:value-of select="OriginalWeight/@amt"/>
/<xsl:value-of select="@issuedate"/>
/<xsl:value-of select="@currency"/>
</xsl:template>

but that will give me extra linebreaks on the output. How do I avoid those?

Was it helpful?

Solution

How about:

<xsl:template match="CAInfo">
  <xsl:value-of select="concat('PRE/', @prefix, '-', @serial, '/', @orig, @dest, '/',
      RevisedWeight/@code, RevisedWeight/@amt, '/', OriginalWeight/@code, 
      OriginalWeight/@amt, '/', @issuedate, '/', @currency)"/>
</xsl:template>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top