<?xml version="1.0" encoding="windows-1251"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h1>My first report</h1> <h2></h2> <table border="1"> <tr bgcolor="#9acd32"> <th>TYPE</th> <th>AZ1</th> <th>H</th> <th>POLAR</th> <th>IMAGE</th> <th>POWER</th> <th>KONTR</th> </tr> <xsl:for-each select="Root/Table1/Row0"> <tr> <td> <xsl:value-of select="TYPE"/> </td> <td> <xsl:value-of select="AZ1"/> </td> <td> <xsl:value-of select="H"/> </td> <td> <xsl:value-of select="POLAR"/> </td> <td> <xsl:value-of select="IMAGE"/> </td> <td> <xsl:value-of select="POWER"/> </td> <td> <xsl:value-of select="KONTR"/> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template></xsl:stylesheet>
QDomDocument doc; QFile file("d:\\dbf\\xsl.xsl"); if (!file.open(QIODevice::ReadOnly)) return; if (!doc.setContent(&file)) { file.close(); return; } file.close(); QDomElement deRoot = doc.documentElement(); QDomElement deNode = deRoot.namedItem("h1").toElement(); bool _is_null = deNode.isNull();
QDomElement deRoot = doc.documentElement();QDomElement deNode = deRoot.namedItem("body").toElement();bool _is_null = deNode.isNull();
void QModelDOMParser::saveXSL(){ QFile file; QTextStream stream(&file); file.setFileName("d:\\dbf\\xsl.xsl"); file.open(QFile::WriteOnly); QDomDocument doc; QDomProcessingInstruction xmlDeclaration = doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"windows-1251\""); doc.appendChild(xmlDeclaration); QDomElement xslElement = doc.createElement("xsl:stylesheet"); xslElement.setAttribute("version", "1.0"); xslElement.setAttribute("xmlns:xsl", "http://www.w3.org/1999/XSL/Transform"); doc.appendChild(xslElement); QDomElement xslTemplate = doc.createElement("xsl:template"); xslTemplate.setAttribute("match", "/"); xslElement.appendChild(xslTemplate); QDomElement htmlEl = doc.createElement("html"); xslTemplate.appendChild(htmlEl); QDomElement bodyEl = doc.createElement("body"); htmlEl.appendChild(bodyEl); QDomElement h1El = doc.createElement("h1"); QDomText h1Text = doc.createTextNode("My first report"); h1El.appendChild(h1Text); bodyEl.appendChild(h1El); QDomElement h2El = doc.createElement("h2"); QDomText h2Text = doc.createTextNode(objectName); h2El.appendChild(h2Text); bodyEl.appendChild(h2El); QDomElement tableEl = doc.createElement("table"); tableEl.setAttribute("border", "1"); bodyEl.appendChild(tableEl); QDomElement trEl = doc.createElement("tr"); trEl.setAttribute("bgcolor", "#9acd32"); tableEl.appendChild(trEl); for (int i = 0; i < myModel->columnCount(); ++i) { QDomElement thEl = doc.createElement("th"); QDomText thText = doc.createTextNode(myModel->headerData(i, Qt::Horizontal).toString()); thEl.appendChild(thText); trEl.appendChild(thEl); } QDomElement xslForEachEl = doc.createElement("xsl:for-each"); xslForEachEl.setAttribute("select", "Root/Table1/Row0"); tableEl.appendChild(xslForEachEl); QDomElement trEl_u = doc.createElement("tr"); xslForEachEl.appendChild(trEl_u); for (int i = 0; i < myModel->columnCount(); ++i) { QDomElement td = doc.createElement("td"); trEl_u.appendChild(td); QDomElement xslValueOfEl = doc.createElement("xsl:value-of"); QDomText xslValueText = doc.createTextNode(myModel->headerData(i, Qt::Horizontal).toString()); xslValueOfEl.setAttribute("select", myModel->headerData(i, Qt::Horizontal).toString()); td.appendChild(xslValueOfEl); } stream << doc.toString(); file.close();}
QDomNode n = doc.firstChild(); while (!n.isNull()) { if (n.isElement()) { QDomElement e = n.toElement(); qDebug() << "Element name: " << e.tagName() << endl; break; } n = n.nextSibling(); }
QDomNodeList tmpNodeList = doc.elementsByTagName("h1"); /// теперь в tmpNodeList лежит список всех тэгов h1QDomNode tmpNode = tmpNodeList.at(0); ///берешь первый по списку тэг h1, либо в цикле пробегаешь по всемQString tmpStr = tmpNode.toElement().text(); ///вуаля, все лежит на месте
QDomElement Root = doc.documentElement(); QDomNodeList deNode = Root.childNodes(); QDomNode body = deNode.at(0).firstChild().firstChild();//iterate to <body> element QString nodeName = body.nodeName();//make sure it is "body" indeed