EEAAO
EEAAO

Reputation: 1

XSLT Need to Limit Return of Multiple Instances in XML File to 18 Characters

I currently have the following code to combine multiple instances of Ustrd into one returned value:

<Ustrd>
    <xsl:value-of select="a:RmtInf/a:Ustrd"/>
</Ustrd>

This returns:

<Ustrd>Item-1 Item-2 Item-3</Ustrd>

The problem is that I need to limit this to 18 characters, and the substring function does not work with a sequence of items.

Tried:

<Ustrd>
    <xsl:value-of select="substring(a:RmtInf/a:Ustrd, 1, 18"/>
</Ustrd>

Expected Result:

<Ustrd>Item-1 Item-2 Item</Ustrd>

Upvotes: 0

Views: 146

Answers (2)

Sebastien
Sebastien

Reputation: 2714

Here's a way this could be done in XSLT 1.0.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <Ustrd>
        <xsl:variable name="temp">
          <xsl:for-each select="RmtInf/Ustrd">
            <xsl:value-of select="."/>
            <xsl:if test="position()!=last()">
              <xsl:value-of select="' '"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:variable>
        
        <xsl:value-of select="substring($temp,1,18)"/>
    </Ustrd>
  </xsl:template>
  
</xsl:stylesheet>

(Only need to add your namespace.)

See it working here: https://xsltfiddle.liberty-development.net/pPgzCL4

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167696

Use string-join first e.g. substring(string-join(a:RmtInf/a:Ustrd, ' '), 1, 18). In XPath 3.1 you can also write that as a:RmtInf/a:Ustrd => string-join(' ') => substring(1, 18).

Upvotes: 1

Related Questions