Tags

, ,


We can create Html layout by XSLT from XML. So you don’t have to depend on XML content that in times might retrieved for a webservice so you are not sure about the content of the xml but you are sure about the structure of the xml.

So, We can make an XSLT depending on XML structure and XSLT would provide the look and feel for the HTML depending on the structure.

Here,

I will proved an example by creating an table form an xml by xslt.

So for this we will need to do the following things:

1) Get the XML ( here we will have an example xml)

2) Writing the XSLT

3) Convert the XML using XSLT

4) Showing the output

 

XML:
This is an XML collected form an webservice response:

<?xml version=1.0encoding=utf-8?> <GetSubmittedFormsWithFieldsResult> <SubmittedFormDTO> <FieldList> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Adresse_1</FieldName> <FieldValue>My Address 1</FieldValue> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Journalnummer</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>StX</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Datoogunderskrift_1</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Adresse</FieldName> <FieldValu>Address of form</FieldValu> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Navn</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Navn_1</FieldName> <FieldValue>Altaf</FieldValue> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Dato</FieldName> <FieldValue>12/12/12</FieldValue> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Personnummerfdselsdato_1</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Personnummerfdselsdato</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Datoogunderskrift</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>IndleverestilstatsamtetiKbenhavn</FieldName> <FieldValue>dfgdfgd</FieldValue> </SubmittedFormFieldDTO> </FieldList> <ReferenceString /> <Id>24</Id> <FormId>224</FormId> <SubmittedTime>2009-06-02T13:05:38</SubmittedTime> <SurveyID>0</SurveyID> <SurveyName /> <SurveyRecipientGroupID>0</SurveyRecipientGroupID> <UserEmail /> <UserIp>192.168.20.189</UserIp> <ReferenceNumber>11745</ReferenceNumber> <WorkflowStepId>0</WorkflowStepId> <WorkflowStepTitle /> <StatusCodeId>1</StatusCodeId> <StatusCode>0</StatusCode> <IndsenderId>farhana@leads-bd.com</IndsenderId> <StatusCodeText /> <SessionNote /> <ReferenceNumberString /> <FormLink /> <SerialNumber>-1</SerialNumber> <IsSubmitted>false</IsSubmitted> <FormAlias>vi085 PDF</FormAlias> </SubmittedFormDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Personnummerfdselsdato_1</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Personnummerfdselsdato</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Datoogunderskrift</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>IndleverestilstatsamtetiKbenhavn</FieldName> <FieldValue>afafa</FieldValue> </SubmittedFormFieldDTO> </FieldList> <ReferenceString /> <Id>32</Id> <FormId>224</FormId> <SubmittedTime>2009-06-02T13:55:47.47</SubmittedTime> <SurveyID>0</SurveyID> <SurveyName /> <SurveyRecipientGroupID>0</SurveyRecipientGroupID> <UserEmail>farhana@leads-bd.com</UserEmail> <UserIp>192.168.20.189</UserIp> <ReferenceNumber>11761</ReferenceNumber> <WorkflowStepId>34</WorkflowStepId> <WorkflowStepTitle /> <StatusCodeId>2</StatusCodeId> <StatusCode>0</StatusCode> <IndsenderId>farhana</IndsenderId> <StatusCodeText /> <SessionNote /> <ReferenceNumberString /> <FormLink /> <SerialNumber>18</SerialNumber> <IsSubmitted>true</IsSubmitted> <FormAlias>vi085 PDF</FormAlias> </SubmittedFormDTO> <SubmittedFormDTO> <FieldList> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Adresse_1</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Journalnummer</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>StX</FieldName> <FieldValue>forældre</FieldValue> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Datoogunderskrift_1</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Adresse</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Navn</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Navn_1</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Dato</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Personnummerfdselsdato_1</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Personnummerfdselsdato</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>Datoogunderskrift</FieldName> <FieldValue /> </SubmittedFormFieldDTO> <SubmittedFormFieldDTO> <Id>0</Id> <PageNumber>1</PageNumber> <FormPageId>0</FormPageId> <FieldName>IndleverestilstatsamtetiKbenhavn</FieldName> <FieldValue>hfdgdfhhfdhfh</FieldValue> </SubmittedFormFieldDTO> </FieldList> <ReferenceString /> <Id>43</Id> <FormId>224</FormId> <SubmittedTime>2009-06-02T14:49:20</SubmittedTime> <SurveyID>0</SurveyID> <SurveyName /> <SurveyRecipientGroupID>0</SurveyRecipientGroupID> <UserEmail>rakibul_islam@leads-bd.com</UserEmail> <UserIp>192.168.20.185</UserIp> <ReferenceNumber>11784</ReferenceNumber> <WorkflowStepId>34</WorkflowStepId> <WorkflowStepTitle /> <StatusCodeId>2</StatusCodeId> <StatusCode>0</StatusCode> <IndsenderId>rakibul_islam@leads-bd.com</IndsenderId> <StatusCodeText /> <SessionNote /> <ReferenceNumberString /> <FormLink /> <SerialNumber>27</SerialNumber> <IsSubmitted>true</IsSubmitted> <FormAlias>vi085 PDF</FormAlias> </SubmittedFormDTO>

</GetSubmittedFormsWithFieldsResult>

 

 

 

 

 

Now if we apply following xslt:

 

 

 

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Author: Altaf Hussain; SE; LeadSoft BD. Ltd-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <h1 style="font-family:calibri; color:#33CCFF">Submitted Form Details</h1>
        <hr style="color:#33CCFF; width:100%"></hr>
        <table style="background-color:#B7E8FF">
          <tr>
            <td>
              <table style="border:1px solid #33CCFF; font-family:Calibri" bgcolor="transparent" cellspacing="1" cellpadding="2px">
                <tr bgcolor="#B7E8FF" style="border:1px solid #B7E8FF" >
                  <th>FromId</th>
                  <th>Id</th>
                  <xsl:for-each select="//SubmittedFormDTO[1]/FieldList/SubmittedFormFieldDTO">
                    <th>
                      <xsl:value-of select="FieldName"/>
                    </th>
                  </xsl:for-each>
                  <th>ReferenceString</th>
                  <th>SubmittedTime</th>
                  <th>SurveyID</th>
                  <th>SurveyName</th>
                  <th>SurveyRecipientGroupID</th>
                  <th>UserEmail</th>
                  <th>UserIp</th>
                  <th>ReferenceNumber</th>
                  <th>WorkflowStepId</th>
                  <th>StatusCodeId</th>
                  <th>StatusCode</th>
                  <th>IndsenderId</th>
                  <th>StatusCodeText</th>
                  <th>SessionNote</th>
                  <th>ReferenceNumberString</th>
                  <th>FormLink</th>
                  <th>SerialNumber</th>
                  <th>IsSubmitted</th>
                  <th>FormAlias</th>
                </tr>
                <!--For Value-->

                <xsl:for-each select="//FieldList">
                  <tr  align="Justify" style="border:1px solid #B7E8FF">
                    <td style="background-color:white">
                      <xsl:value-of select="../FormId"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../Id"/>
                    </td>
                    <xsl:for-each select="SubmittedFormFieldDTO">
                      <td style="background-color:white" align="Middle">
                        <xsl:value-of select="FieldValue"/>
                      </td>
                    </xsl:for-each>
                    <td style="background-color:white">
                      <xsl:value-of select="../ReferenceString"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../SubmittedTime"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../SurveyID"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../SurveyName"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../SurveyRecipientGroupID"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../UserEmail"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../UserIp"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../ReferenceNumber"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../WorkflowStepId"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../StatusCodeId"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../StatusCode"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../IndsenderId"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../StatusCodeText"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../SessionNote"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../ReferenceNumberString"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../FormLink"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../SerialNumber"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../IsSubmitted"/>
                    </td>
                    <td style="background-color:white">
                      <xsl:value-of select="../FormAlias"/>
                    </td>
                  </tr>
                </xsl:for-each>
              </table>
            </td>
          </tr>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

 

This will provide the following Tabular Output in HTML:

image

Fig1: First part of the output

image

Fig2: Second part of the output

 

Now the Conversion Function:

private void ConvertSumbittedFormSeriesDTOWithoutSoapHeader()
       {
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(MapPath("XmlFileName.xml"));
           string xslPath = Server.MapPath("XsltFileName.xslt");
           XslCompiledTransform transform = new XslCompiledTransform();
           transform.Load(xslPath);
           transform.Transform(xmlDoc, null, Response.Output);
       }

 

Advertisements