arulapp
arulapp

Reputation: 41

How to create a new entry based on a condition in xslt

Input XML:

.....
..... <!-- Many lines of code -->
.....
<Attribute Name="Attr1">  
  <Array>
       <Object Type="Object1">
          <Attribute Name="Attr2">
              "123"
              "234"
           </Attribute>
           <Attribute Name="Attr3">"456"</Attribute>
        </Object>
        <Object Type="Object2">
           <Attribute Name="Attr4">
               "444"
               "555"
            </Attribute>               
         </Object>
         <Object Type="Object3">
            <Attribute Name="Attr5">
                "666"   
                "777"
                "888"     
    <!-- My new item should come here -->   
             </Attribute>
          </Object> 
   </Array>
</Attribute>

I tried the following XSLT to add a new entry(999) to the above mentioned location "My new item should come here". Before inserting this new item, I want to check if the node, Attribute(<Attribute Name = "Attr5") has a value "888". Only if it contains "888" , I should insert "999" after that. can you please let me knoe how this can be achieved?

    <?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:copy>
       <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
     </xsl:template>

     <xsl:variable name="obj" select='"Object3"'/>
     <xsl:variable name="attr" select='"Attr5"'/>
     <xsl:param name="evalue">"999"</xsl:param>

     <xsl:template match="Attribute/Array/Object[@Type=$obj]/Attribute[@Name=$attr]">
      <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:value-of select="." />
    <xsl:value-of select="$evalue"/>
  </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>

Output XML

<Attribute Name="Attr1">  
  <Array>
       <Object Type="Object1">
          <Attribute Name="Attr2">
              "123"
              "234"
           </Attribute>
           <Attribute Name="Attr3">"456"</Attribute>
        </Object>
        <Object Type="Object2">
           <Attribute Name="Attr4">
               "444"
               "555"
            </Attribute>               
         </Object>
         <Object Type="Object3">
            <Attribute Name="Attr5">
                "666"   
                "777"
                "888"     
                "999"
             </Attribute>
          </Object> 
   </Array>
</Attribute>

This is how the final XML should look like. Please help me out

Upvotes: 1

Views: 155

Answers (1)

Erlock
Erlock

Reputation: 1968

It lacks many instructions in your template...

<xsl:template match="Attribute/Array/Object[@Type=$obj]/Attribute[@Name=$attr and contains(., '&quot;888&quot;')]">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:value-of select="." />
    <xsl:value-of select="$evalue"/>
  </xsl:copy>
</xsl:template>

Upvotes: 2

Related Questions