locked
Problem with xml and Xslt data transfer...help. RRS feed

  • Question

  • User-507786106 posted

    I am trying to retrieve data from XML into XSLT and I am only able to get the record ID value all other nodes appears as static text.... Please review the codes below and share with me what I could possibly be doing wrong. Thanks

    Xml CODE

    XmlDocument doc = new XmlDocument();
    XmlNode oNode = null;
    
    XmlDeclaration oDeclaration = doc.CreateXmlDeclaration("1.0""UTF-8"null); doc.AppendChild(oDeclaration);                  XmlElement root = doc.CreateElement("IDRoot""XNamespace.Xmlns=http://www.w3.org/2001/XMLSchema-instance" + " xsi:noNamespaceSchemaLocation=" + "http://localhost:3465/TestSchema.xsd"); doc.AppendChild(root); //Creating First Element(Version) XmlElement IDNode = doc.CreateElement("PID""XNamespace.Xmlns=http://www.w3.org/2001/XMLSchema-instance" + " xsi:noNamespaceSchemaLocation=" + "http://localhost:3465/TestSchema.xsd"); oNode = doc.CreateElement("PID"); oNode.InnerText= PID.ToString(CultureInfo.InvariantCulture); IDNode .AppendChild(oNode);                 XmlElement ProjectNode = doc.CreateElement("ProjectName""XNamespace.Xmlns=http://www.w3.org/2001/XMLSchema-instance" + " xsi:noNamespaceSchemaLocation=" + "http://localhost:3465/TestSchema.xsd"); oNode = doc.CreateElement("ProjectName");
    oNode.InnerText= _projectName.ToString(CultureInfo.InvariantCulture); ProjectNode.AppendChild(oNode);

    doc.Save(@"C:\prpject\myxml.xml");

    XslTransform --- works but only PID data value is carried over to XSLT file

    XslCompiledTransform xslt = new XslCompiledTransform();
     xslt.Load(xslfilename);
     
    // Transform the file and output an HTML string. 
    string HTMLoutput;
    StringWriter writer = new StringWriter();
    xslt.Transform(xmlFileName, null, writer);
    HTMLoutput = writer.ToString();
    

    XSLT output

    <?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" xmlns:user="urn:user">
      <xsl:output method="html" encoding="utf-8"  indent="no"/>
      <xsl:template match="IDRoot">
      <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;</xsl:text>
       <html>
         <head>
           <title>
             <xsl:value-of select="Subject" />
           </title>
         </head>
          <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
            <h2>Comment </h2>
            <table width="width: 100%;" cellpadding="0" cellspacing="0" border="1">
              <tr valign="top" bgcolor="#9acd32">
                <th style="width:200px;"></th>
                <th style="width:85%;"></th>
              </tr>
             <xsl:for-each select="ITBRoot"
              <tr>
                <td width="200">
                  <div style="font-size: 12pt">
                    <b>Project ID:</b>
                  </div>
                </td>
                <td>
                  <xsl:value-of select="PID"/>  <---DOES show the data value
                  <Output>
                    <xsl:attribute name="PID"> <---DOES show the data value
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="PID"/> <---DOES show the data value
                    </xsl:attribute>
                  </Output>
                </td>
              </tr>
              
                <tr>
                  <td width="200">
                    <div style="font-size: 12pt">
                      <b>Project Name:</b>
                    </div>
                  </td>
                  <td>
                    <xsl:value-of select="ProjectName"/>  <---does not show the data value
                    <Output>
                      <xsl:attribute name="ProjectName">  <---does not show the data value
                        <xsl:text> </xsl:text>
                        <xsl:value-of select="ProjectName"/> <---does not show the data value
                      </xsl:attribute>
                    </Output>
                  </td>
                </tr>
      </xsl:template>
    </xsl:stylesheet>
     

    Tuesday, April 14, 2015 6:44 PM

Answers

  • User-271186128 posted

    Hi slimbunny,

    //Creating First Element(Version)
    XmlElement IDNode = doc.CreateElement("PID""XNamespace.Xmlns=http://www.w3.org/2001/XMLSchema-instance" + " xsi:noNamespaceSchemaLocation=" + "http://localhost:3465/TestSchema.xsd");
    oNode = doc.CreateElement("PID");
    oNode.InnerText= PID.ToString(CultureInfo.InvariantCulture);
    IDNode .AppendChild(oNode);
                   
     
    XmlElement ProjectNode = doc.CreateElement("ProjectName""XNamespace.Xmlns=http://www.w3.org/2001/XMLSchema-instance" + " xsi:noNamespaceSchemaLocation=" + "http://localhost:3465/TestSchema.xsd");
    oNode = doc.CreateElement("ProjectName");

    oNode.InnerText= _projectName.ToString(CultureInfo.InvariantCulture);
    ProjectNode.AppendChild(oNode);
    doc.Save(@"C:\prpject\myxml.xml");

    As for this issue, please check the above code, and modify it as below:

    XmlElement ProjectNode = doc.CreateElement("ProjectName", "XNamespace.Xmlns=http://www.w3.org/2001/XMLSchema-instance" + " xsi:noNamespaceSchemaLocation=" + "http://localhost:3465/TestSchema.xsd");
    oNode = doc.CreateElement("ProjectName");
    
    oNode.InnerText= ProjectName.ToString(CultureInfo.InvariantCulture);
    
    ProjectNode.AppendChild(oNode);
    doc.Save(@"C:\prpject\myxml.xml"); 

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2015 6:13 AM