Blossom
Blossom

Reputation: 127

Copy all node exclude modified node

I have an xml as below:

<root>
  <book>abcd</book>
  <pen>ghi</pen>
  <source Qual="poor" vise="n">

    <source>
      <input value="in"></input>
      <input></input>
    </source>

  </source>
  <class>ab</class>
  <source>noaatributes</source>
  <studio>ghi</studio>
  <source Qual="good" vise="m">

    <source>
      <input></input>
      <input value="out"></input>
    </source>

  </source>
</root>

I would like to convert the xml in the below format :

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <book>abcd</book>
  <pen>ghi</pen>
  <poor value="in"/>


  <class>ab</class>
  <source>noaatributes</source>
  <studio>ghi</studio>
  <good value="out"/>



</root>

but the code i have written is not giving the required output..I have got stuck in middle of my coding..Any suggestions pls. Here is the code i have written so far :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">


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



  </xsl:template>


  <xsl:template match="source/@Qual">
    <xsl:text disable-output-escaping="yes" ><![CDATA[  
    <]]></xsl:text>
    <xsl:value-of disable-output-escaping="yes" select="."/>
  </xsl:template>

  <xsl:template match="source/source">
    <xsl:apply-templates select="input"/>
    <xsl:text disable-output-escaping="yes" ><![CDATA[  
    />]]></xsl:text>
  </xsl:template>

  <xsl:template match="input">
    <xsl:if test ="string-length(./@value) &gt; 0">
      <xsl:text disable-output-escaping="yes" ><![CDATA[    
      ]]></xsl:text>
      <xsl:text disable-output-escaping="yes" ><![CDATA[Value="]]></xsl:text>
      <xsl:value-of disable-output-escaping="yes" select="./@value"/>
      <xsl:text disable-output-escaping="yes" ><![CDATA[" ]]></xsl:text>
    </xsl:if>

  </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 63

Answers (2)

Linga Murthy C S
Linga Murthy C S

Reputation: 5432

This is going to be little smaller

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="root/source[@Qual]">
    <xsl:element name="{@Qual[1]}">
        <xsl:copy-of select="source/input/@value[1]"/>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

Upvotes: 1

helderdarocha
helderdarocha

Reputation: 23637

To obtain the output you want, it can be much simpler:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="*[not(source[@Qual])]">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="source[@Qual]">
        <xsl:element name="{@Qual}">
            <xsl:attribute name="value">
                <xsl:value-of select="source/input/@value" />
            </xsl:attribute>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 0

Related Questions