-2

i have this xml file....i have to check the status of SE/SSE ...if it is active then it will get inside the Details tag....it will read the status...if it is active then it will read the details tag , unless it ll discard that details node. Like that if the SSE status is "InACTIVE", no need to read inside that Node.

<Employees>
<Employee>
    <SE>
        <Name>bikash</Name>
        <dept>DY</dept>
        <status>ACTIVE</status>
     <Details dataStr="list">
        <status>ACTIVE</status>
        <address>India</address>
        <streetNo>19</streetNo>
     </Details>
     <Details dataStr="list">
        <status>InACTIVE</status>
        <address>CHINA</address>
        <streetNo>20</streetNo>
     </Details>
     <area>BLORE</area>
     <SEIdCount>1</SEIdCount>
  </SE>
  <SSE>
        <status>InACTIVE</status>
     <emplNo>23</emplNo>
     <Details dataStr="list">
        <status>InActive</status>
        <absent>y</absent>
     </Details>
     <Details dataStr="list">
        <status>Active</status>
        <name>anu</NAME>
     </Details>
    <area>CHN</area>
    <SEIdCount>2</SEIdCount>
  </SSE>
 </Employee> 
</Employees>

My Expected response is this

<Employees>
<Employee>
    <SE>
        <Name>bikash</Name>
        <dept>DY</dept>
        <status>ACTIVE</status>
     <Details dataStr="list">
        <status>ACTIVE</status>
        <address>India</address>
        <streetNo>19</streetNo>
     </Details>
     <area>BLORE</area>
     <SEIdCount>1</SEIdCount>
  </SE>
 </Employee> 
</Employees>
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
mohan
  • 447
  • 4
  • 11
  • 26

2 Answers2

1

Try:

XSLT 1.0

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

<xsl:template match="/Employees">
    <Employees>
        <xsl:for-each select="Employee/SE">
            <Employee>
                <SE>
                    <xsl:copy-of select="Name | dept | status | Details[status='Active'] | area | SEIdCount"/>
                </SE>
            </Employee> 
        </xsl:for-each> 
    </Employees>
</xsl:template>

</xsl:stylesheet>

Applied to a well-formed XML input:

<Employees>
  <Employee>
    <SE>
      <Name>bikash</Name>
      <dept>DY</dept>
      <status>Active</status>
      <Details dataStr="list">
        <status>Active</status>
        <address>India</address>
        <streetNo>19</streetNo>
      </Details>
      <Details dataStr="list">
        <status>InACTIVE</status>
        <address>CHINA</address>
        <streetNo>20</streetNo>
      </Details>
      <area>BLORE</area>
      <SEIdCount>1</SEIdCount>
    </SE>
    <SSE>
      <status>InACTIVE</status>
      <emplNo>23</emplNo>
      <Details dataStr="list">
        <status>InActive</status>
        <absent>y</absent>
      </Details>
      <Details dataStr="list">
        <status>Active</status>
        <name>anu</name>
      </Details>
      <area>CHN</area>
      <SEIdCount>2</SEIdCount>
    </SSE>
  </Employee>
</Employees>

this will return:

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
  <Employee>
    <SE>
      <Name>bikash</Name>
      <dept>DY</dept>
      <status>Active</status>
      <Details dataStr="list">
        <status>Active</status>
        <address>India</address>
        <streetNo>19</streetNo>
      </Details>
      <area>BLORE</area>
      <SEIdCount>1</SEIdCount>
    </SE>
  </Employee>
</Employees>

Note: XML is case-sensitive:

select="Name" will not select <name>, and "Active" is not the same thing as "ACTIVE".

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
-1

Using XSLT 1.0 You can also try this one out if you don't have the number child nodes within SE node fixed

  <xsl:template match="/*/Employee">
    <Employees>
      <Employee>
        <SE>
          <xsl:for-each select="*">
            <xsl:choose>
              <xsl:when test="status='ACTIVE'">
                <xsl:for-each select="./*">
                  <xsl:choose>
                    <xsl:when test="local-name(.)='Details' and ./status='ACTIVE'">
                      <xsl:copy-of select="."/>
                    </xsl:when>
                    <xsl:when test="local-name(.)='Details' and ./status='InACTIVE'"/>
                    <xsl:otherwise>
                      <xsl:copy-of select="."/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:for-each>
              </xsl:when>
            </xsl:choose>
          </xsl:for-each>
        </SE>
      </Employee>
    </Employees>
  </xsl:template>
Saurav
  • 592
  • 4
  • 21
  • Thanks everyone for your valuable comments.....i got the solution by referring your comments. Thanks @Saurav...thnx a lot – mohan Feb 02 '15 at 11:30
  • I want to add 365 days to today's date, in my xslt i am printing today's date like this "" – mohan Feb 10 '15 at 06:30