Add Space Before a tag if it is Present in XSLT

Iam creating styles using XSLT for my XML file.

Here is my code:

<xsl:template match="LEXVAR">
  <xsl:copy>
    <xsl:apply-templates/>
      <xsl:choose>
        <xsl:when test="./preceding-sibling::LEXVAR">
          <span class="neutral">
            <xsl:text>, </xsl:text>
          </span>
        </xsl:when>
       <xsl:when test="./preceding-sibling::*">
         <span class="neutral">
           <xsl:text> </xsl:text>
         </span>
       </xsl:when>
     <xsl:otherwise/>
   </xsl:choose>
  </xsl:copy>
</xsl:template>

In my case: LEXVAR is tag and it is to be styled, the condition for styling is add a space if any other tag is present before to it or no space is needed.

And the above code is not working, iam new to XSl, can you please help me.? Thanks in advance.

Upvotes: 1

Views: 358

Answers (2)

Try this to solve your issue :

<xsl:template match="LEXVAR">
    <span class="exp">
        <xsl:if test="./preceding-sibling::*">
            <xsl:text> </xsl:text>
        </xsl:if>
    <xsl:text></xsl:text>
        <xsl:apply-templates/>
    </span>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167571

I would put the condition into a predicate of the match pattern e.g.

<xsl:template match="LEXVAR[preceding-sibling::*]">
  <span><xsl:text> </xsl:text></span>
  <xsl:next-match/>
</xsl:template>

Then make sure you have an identity transformation template or other template handing other LEXVAR elements so that the next-match has some template to call.

Upvotes: 1

Related Questions