<xml>
<head>
<info>
<content>
        <source attribute1="RSC1985s5c1" attribute2="6(17)">
            data1
        </source>
        <cite/>
        <case/>
        (
        <target attribute1="LRC1985s5c1" attribute1="6(17)1">
        3e/191
        </target>
        )
</content>

<content>
    <source attribute1="RSC1985s5c1" attribute2="6(17)">
     data2
    </source>
    <cite/>
    <case/>
    (
    <target attribute1="LRC1985s5c4" attribute2="6(17)1">
    4e/54
    </target>
    )
</content>
</info>
</head>
</xml>

What i want is to merge the content of nodes in to single contetnt node(Asbelow), output should be like

<xml>
<head>
<info>
<content>
        <source attribute1="RSC1985s5c1" attribute2="6(17)">
         data
         </source>
         <source attribute1="RSC1985s5c1" attribute2="6(17)">
           data2
          </source>
          <cite/>
          <case/>
          <target attribute1="LRC1985s5c1" attribute1="6(17)1">
          3e/191
          </target>
          )
          <source attribute1="RSC1985s5c1" attribute2="6(17)">
          data2
          </source>
          <cite/>
          <case/>
          (
          <target attribute1="LRC1985s5c4" attribute2="6(17)1">
          4e/54
          </target>
          )
</content>
</info>
</head>
</xml>

Recommended Answers

All 4 Replies

What exactly are the brackets for in the xml? are they typos? i.e.

(
<target attribute1="LRC1985s5c4" attribute2="6(17)1">
4e/54
</target>
)

they can be treat as data

Ok, working on it currently, have my own little issue with it which im looking for a solution too and then its done :)

Xslt:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="info">
    <xsl:copy>
      <xsl:element name="content">
        <xsl:for-each select="content">
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates select="node()"/>
        </xsl:for-each>
      </xsl:element>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Applied to the given XML produces:

<xml>
  <head>
    <info>
      <content>
        <source attribute1="RSC1985s5c1" attribute2="6(17)">
          data1
        </source>
        <cite />
        <case />
        (
        <target attribute1="LRC1985s5c1" attribute2="6(17)1">
          3e/191
        </target>
        )
      <source attribute1="RSC1985s5c1" attribute2="6(17)">
          data2
        </source><cite /><case />
        (
        <target attribute1="LRC1985s5c4" attribute2="6(17)1">
          4e/54
        </target>
        )
      </content>
    </info>
  </head>
</xml>

There is no standard for how indentation is dealt with and so the odd indentation on some elements may not be the same on your xslt processor. The format you desired however is achieved.

Credit goes to Dimitre Novatchev on StackOverflow for solving my issue I had when doing this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.