Home > Enterprise >  XSLT For-each to display in a semicolon-delimited value instead of separate tags
XSLT For-each to display in a semicolon-delimited value instead of separate tags

Time:01-21

is there a way to list all "for-each" values in XSLT in a single tag but semicolon-delimited value, instead of multiple tags with multiple values? For example:

XSLT

<xsl:for-each select="/StringAssetInfoMulti[attrTagName='LeadActor']/values/s">
  <xsl:element name="{'LeadActor'}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:for-each>

XML to transform

<StringAssetInfoMulti attrId.l="2870">
    <attrTagName t="ws">LeadActor</attrTagName>
    <attrName t="ws">LeadActor</attrName>
        <values t="lws">
            <s>Rufa Mae Quinto</s>
            <s>Edgar Allan Guzman</s>
            <s>Angelina Kanapi</s>
            <s>Barbie Capacio</s>
            <s>Jude Matthew Servilla</s>
        </values>
</StringAssetInfoMulti>

So, instead of transforming the LeadActors tag into the result below:

<LeadActor>Rufa Mae Quinto</LeadActor>
<LeadActor>Edgar Allan Guzman</LeadActor>
<LeadActor>Angelina Kanapi</LeadActor>
<LeadActor>Barbie Capacio</LeadActor>
<LeadActor>Jude Matthew Servilla</LeadActor>

I need a transformation that can consolidate all results into a single tag with semicolon-delimited values:

<LeadActor>Rufa Mae Quinto;Edgar Allan Guzman;Angelina Kanapi;Barbie Capacio;Jude Matthew Servilla</LeadActor>

Is that possible and what will I put into the XSLT? Thank you!

CodePudding user response:

In XSLT 2 and later you can use

<LeadActor>
  <xsl:value-of select="/StringAssetInfoMulti[attrTagName='LeadActor']/values/s" separator=";"/>
</LeadActor>

In XSLT 1 it is kind of obvious that you need to use for-each or apply-templates processing and then output the ; as a separator with <xsl:text>;</xsl:text> for either all but the last or all but the first item e.g.

<xsl:for-each select="/StringAssetInfoMulti[attrTagName='LeadActor']/values/s">
  <xsl:if test="position() > 1">
    <xsl:text>;</xsl:text>
  </xsl:if>
  <xsl:value-of select="."/>
</xsl:for-each>
  •  Tags:  
  • Related