none
What;s wrong with my XSLT? RRS feed

  • Question

  • Hi, 
    I have an XML document from which I have to exclude records with all their child elements empty.

    <Employees>
      <Employee>
        <FirstName>Sam</FirstName>
      </Employee>
      <Employee>
        <FirstName></FirstName>
      </Employee>
    </Employees>
     
      So the resulting XML Document have to look like this:  
    <Employees>
      <Employee>
        <FirstName>Sam</FirstName>
      </Employee>
    </Employees>


    After whole day of banging with it I came up with the following XSLT:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="@*|node()">
        <xsl:if test=". != ''">
          <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
        </xsl:if>
      </xsl:template>
    </xsl:stylesheet>
    which still gives me not what I want - here is what I am getting, note that empty <Employee/> record: 

    <Employees>
      <Employee>
        <FirstName>Sam</FirstName>
      </Employee>
      <Employee/>
    </Employees>
    Somebody, please, how do I modify the XSLT to get rid of whole Employee record, not only its empty elements? Thank you very much!




    • Edited by fly2 Thursday, September 4, 2014 1:15 AM
    Thursday, September 4, 2014 1:09 AM

Answers

  • Hello fly2,

    Which class do you use to transform your XML file? Because I use the System.Xml.Xsl.XslTransform to transform the XML file with the XSLT file all provided by you, the result is what your expected.

    Here is the transform code:

    static void Main(string[] args)
    
            {
    
                System.Xml.Xsl.XslTransform transform = new System.Xml.Xsl.XslTransform();
    
    
                transform.Load(@"D:\BMX\Project\2014\09\XMLs\P20140904\Sample20140904.xslt");
    
    
                transform.Transform(@"D:\BMX\Project\2014\09\XMLs\P20140904\Sample20140904.xml", @"D:\BMX\Project\2014\09\XMLs\P20140904\OutPut.xml");
    
            }
    

    The result:

    Please try it.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by fly2 Monday, September 8, 2014 3:15 PM
    Thursday, September 4, 2014 3:34 AM
    Moderator

All replies

  • Hello fly2,

    Which class do you use to transform your XML file? Because I use the System.Xml.Xsl.XslTransform to transform the XML file with the XSLT file all provided by you, the result is what your expected.

    Here is the transform code:

    static void Main(string[] args)
    
            {
    
                System.Xml.Xsl.XslTransform transform = new System.Xml.Xsl.XslTransform();
    
    
                transform.Load(@"D:\BMX\Project\2014\09\XMLs\P20140904\Sample20140904.xslt");
    
    
                transform.Transform(@"D:\BMX\Project\2014\09\XMLs\P20140904\Sample20140904.xml", @"D:\BMX\Project\2014\09\XMLs\P20140904\OutPut.xml");
    
            }
    

    The result:

    Please try it.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by fly2 Monday, September 8, 2014 3:15 PM
    Thursday, September 4, 2014 3:34 AM
    Moderator
  • I am using a custom XSLT in the BizTalk mapper. Surprisingly, your code works fine, but not in the mapper. 

    I am digging it further. Thank you for your help!

    Monday, September 8, 2014 3:15 PM