osfar
osfar

Reputation: 413

how to get tags name that have data with xslt

I have XML payload that looks like

<payload>
<field1>data1</field1> 
<field2>data2</field2>
<field3>data3</field3>
<field4></field4>
</payload>

I want the function to get name of tags that contains data and convert it to the following result

result : field1,field2,field3

is there a way to achieve that ?

Upvotes: 0

Views: 28

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167506

<xsl:value-of select="/payload/*[normalize-space()]" separator=", "/> should do in XSLT 2 or 3.

Upvotes: 1

zx485
zx485

Reputation: 29022

You can use these two templates in an XSLT-1.0 stylesheet:

<xsl:template match="/">
  result: <xsl:apply-templates select="payload/*[normalize-space(.)]" />
</xsl:template>

<xsl:template match="*">
  <xsl:value-of select='name()' />
  <xsl:if test="position()!=last()"><xsl:text>,</xsl:text></xsl:if>
</xsl:template>

Its output is:

result: field1,field2,field3

Upvotes: 1

Related Questions