0

input xml -

       <Term payInNumberOfDays=\"30\">  
          <Ext  name=\"demo\">my</Ext>
        </Term>
        <Term payInNumberOfDays=\"0\">
          <Ext name=\"demo\">value</Ext>
        </Term>
        <Term payInNumberOfDays=\"0\">
          <Ext name=\"demo\">100</Ext>
        </Term>

I want to check Ext @name="demo"then concatenate all Ext element values with - in between

expected output <MYDC>my-value-100</MYDC>

<xsl:variable name="var:v9">
                  <xsl:for-each select="Term">
                      <xsl:variable name="var:v30" select="userCSharp:LogicalEq(string(Ext/@name) , &quot;demo&quot;)" />
                      <xsl:if test="string($var:v30)='true'">
                          <xsl:if test="position() = 1"> <xsl:value-of select="Ext/text()" /> - </xsl:if>
                      </xsl:if>
                  </xsl:for-each>
                </xsl:variable>
                <MYDC>
                  <xsl:value-of select="$var:v9" />
                </MYDC>
Neo
  • 15,491
  • 59
  • 215
  • 405

1 Answers1

2

I am not sure what the extension function is doing and why you need the variables but in plain XSLT 1.0 you could simply use

<xsl:apply-templates select="Term/Ext[@name = 'demo']"/>

and then

  <xsl:template match="Ext">
      <xsl:if test="position() > 1">-</xsl:if>
      <xsl:value-of select="."/>
  </xsl:template>

see http://xsltfiddle.liberty-development.net/bFukv8m

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="exsl msxml"
    version="1.0">

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

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="root">
      <MYDC>
          <xsl:apply-templates select="Term/Ext[@name = 'demo']"/>
      </MYDC>
  </xsl:template>

  <xsl:template match="Ext">
      <xsl:if test="position() > 1">-</xsl:if>
      <xsl:value-of select="."/>
  </xsl:template>

</xsl:stylesheet>

which transforms

<root>
        <Term payInNumberOfDays="30">  
          <Ext  name="demo">my</Ext>
        </Term>
        <Term payInNumberOfDays="0">
          <Ext name="demo">value</Ext>
        </Term>
        <Term payInNumberOfDays="0">
          <Ext name="demo">100</Ext>
        </Term>
</root> 

into

<MYDC>my-value-100</MYDC>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • can we have more simple answer ? – Neo Feb 24 '18 at 13:07
  • 1
    @Neo: *More simple?* You must be kidding. You've received a basic XSLT 1.0 answer that uses no extensions functions. The core answer is very simple, and it's supplemented with sample input, actual output, a demonstration link, and a leading summary that highlights the key part. Your curt request for a *more simple answer* makes no sense. – kjhughes Feb 24 '18 at 14:15
  • @Neo: Ah, I may have figured out why you thought this answer wasn't simple. See if reading [**For loops vs. appy-templates**](https://stackoverflow.com/questions/6342902/for-loops-vs-apply-templates) doesn't help you past any aversion you have to using pattern matching and templates, which are better but can seem less simple to devs used to imperative programming. – kjhughes Feb 24 '18 at 14:34