kuba1999
kuba1999

Reputation: 519

Convert from a nested to a flattened XML structure using XSLT

I am trying this task in XSLT: Convert XML with nested elements to a less nested XML format.

Convert from:

<example>
 <value>
  aaa
   <value>
    bbb
      <value>
       ccc
      </value>
   </value>
 </value>
</example>

To:

<example>
  <value>aaa</value>
  <value>aaa</value>
  <value>bbb</value>
  <value>bbb</value>
  <value>ccc</value>
  <value>ccc</value>
</example>

I have been trying find the solution, but I have only this:

    <xsl:template match="/">
      <exmaple>
         <xsl:apply-templates/>
      </exmaple>
    </xsl:template>

    <xsl:template match="//value/text()">

     <value><xsl:value-of select="."/></value>
     <value><xsl:value-of select="."/></value>

    </xsl:template>

Result (problem with empty tags):

<exmaple>
<value>
aaa
</value><value>
aaa
</value><value>
bbb
</value><value>
bbb
</value><value>
ccc
</value><value>
ccc
</value><value>
</value><value>
</value><value>
</value><value>
</value>
</exmaple>

Upvotes: 4

Views: 458

Answers (2)

Mads Hansen
Mads Hansen

Reputation: 66714

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

    <xsl:template match="example">
        <xsl:copy>
            <xsl:apply-templates select=".//value"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="value">
        <xsl:copy>
            <xsl:value-of select="normalize-space(text())"/>
        </xsl:copy>
        <xsl:copy>
            <xsl:value-of select="normalize-space(text())"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 2

dfsq
dfsq

Reputation: 193261

Try this template with the XPath //value/text()[1]:

<xsl:template match="//value/text()[1]">
    <value><xsl:value-of select="." /></value>
    <value><xsl:value-of select="." /></value>
</xsl:template>

The trick is that you need to select the first text node from every <value>, since text() will return collection of them.

Upvotes: 1

Related Questions