第一部分
----基本信息
1、 如何顯示xml中的信息,運用xsl:具體如何實現如下:
protected
System.Web.UI.WebControls.Xml myXml;
----
聲明一個xml對象
// 加載xml信息
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath( " book.xml " ));
this .myXml.Document = xmlDoc;
// 加載樣式表
XslTransform xslTrans = new XslTransform();
xslTrans.Load(Server.MapPath( " book.xsl " ));
myXml.Transform = xslTrans;
// 加載xml信息
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath( " book.xml " ));
this .myXml.Document = xmlDoc;
// 加載樣式表
XslTransform xslTrans = new XslTransform();
xslTrans.Load(Server.MapPath( " book.xsl " ));
myXml.Transform = xslTrans;
2、 如果在client端加載讀取信息,操作如下:
1、 通過一定的條件來讀取:
var dom =new ActiveXObject"msxml2.DOMDocument.3.0");
var re;
re = //s/g;
dom.async = false;
dom.validateOnParse = false;
dom.resolveExternals = false;
var xpath,show_message;
dom.load("xml/pensionlist.xml");
xpath="/Persions/persion[@Staff_id='"+ per_id +"']";
node1=dom.selectSingleNode(xpath);
if (node1 != null){
node1.childNodes(0).text----第一個節點的信息
node1.childNodes(9).childNodes(0).text-----第十個節點的第一個子節點信息
}
var re;
re = //s/g;
dom.async = false;
dom.validateOnParse = false;
dom.resolveExternals = false;
var xpath,show_message;
dom.load("xml/pensionlist.xml");
xpath="/Persions/persion[@Staff_id='"+ per_id +"']";
node1=dom.selectSingleNode(xpath);
if (node1 != null){
node1.childNodes(0).text----第一個節點的信息
node1.childNodes(9).childNodes(0).text-----第十個節點的第一個子節點信息
}
2、 無條件的讀取:
var oDoc = new ActiveXObject"Microsoft.XMLDOM");
oDoc.load("1.xml");
var objFile1=oDoc.selectSingleNode("//New_CODE");---直接取得的
strcno=objFile1.text;
var root=oDoc.documentElement;
var objFile=root.selectNodes("//SUB_SEQ");-----可以進行循環的
for(var inti=0;inti < objFile .length;inti++)
{
alert(objFile.item(inti).text);
}
oDoc.load("1.xml");
var objFile1=oDoc.selectSingleNode("//New_CODE");---直接取得的
strcno=objFile1.text;
var root=oDoc.documentElement;
var objFile=root.selectNodes("//SUB_SEQ");-----可以進行循環的
for(var inti=0;inti < objFile .length;inti++)
{
alert(objFile.item(inti).text);
}
3、 新增信息到xml文檔中,操作如下:(server端操作)
第一種方法:
XmlDocument xmlDoc
=
new
XmlDocument();
xmlDoc.Load(Server.MapPath( " book.xml " )); ------- 加載信息
if (xmlDoc.DocumentElement.IsEmpty) {------------判斷是否為空
xmlDoc.LoadXml("<gusetbook/>");----------沒有的話新增
}
XmlNode xmlNote = xmlDoc.DocumentElement.AppendChild(xmlDoc.CreateElement( " book " ));
----- 新增第一個節點:book,下面是加其節點的兩個屬性:時間:date及代號:Staff_id
XmlAttribute xmlattr = xmlDoc.CreateAttribute( " date " ); --- 創建一個屬性date
xmlattr.Value = System.DateTime.Now.ToString( " yyyy-MM-dd HH-mm-ss " ); -- 設定屬性信息
xmlNote.Attributes.Append(xmlattr); ---- 在book節點上新增一個屬性
// 新增另一個屬性,要找出本xml文檔中的最大號,進行比較
XmlElement xmld = xmlDoc.DocumentElement; ---- 聲明一元素
string strmax = "" ;
int count_elem = 0 ;
count_elem = xmld.ChildNodes.Count - 2 ; ----- 找到最大節點的index
XmlElement nodeee = (XmlElement)xmld.ChildNodes[count_elem];──找到此節點並轉類型
strmax = nodeee.GetAttribute( " Staff_id " ); --- 取到想要的信息
xmlattr = xmlDoc.CreateAttribute( " Staff_id " ); ----- 增加些元素
xmlattr.Value = strmax.Substring( 0 , 5 ) + ( int .Parse(strmax.Substring( 5 , 1 )) + 1 ) ;
xmlNote.Attributes.Append(xmlattr);
// 增加一個型式的節點
childnote = xmlNote.AppendChild(xmlDoc.CreateElement( " adress " ));
childnote.InnerText = stradress;
// add skill----增加另一個樣式的節點,此節點又有子節點
childnote = xmlNote.AppendChild(xmlDoc.CreateElement( " P_Skills " ));
for ( int i = 0 ;i < 3 ;i ++ ) {
XmlNode childsnote=null;
childsnote=childnote.AppendChild(xmlDoc.CreateElement("P_Skill" & (i+1)));
childsnote.InnerText="java"+i;
}
// 增加有data樣式的節點,如<message><![CDATA[xxxxxxxx]]> </message>
childnote = xmlNote.AppendChild(xmlDoc.CreateElement( " message " ));
childnote.AppendChild(xmlDoc.CreateCDataSection(strmessage));
// 保存新增後的文件
xmlDoc.Save(Server.MapPath( " book.xml " ));
xmlDoc.Load(Server.MapPath( " book.xml " )); ------- 加載信息
if (xmlDoc.DocumentElement.IsEmpty) {------------判斷是否為空
xmlDoc.LoadXml("<gusetbook/>");----------沒有的話新增
}
XmlNode xmlNote = xmlDoc.DocumentElement.AppendChild(xmlDoc.CreateElement( " book " ));
----- 新增第一個節點:book,下面是加其節點的兩個屬性:時間:date及代號:Staff_id
XmlAttribute xmlattr = xmlDoc.CreateAttribute( " date " ); --- 創建一個屬性date
xmlattr.Value = System.DateTime.Now.ToString( " yyyy-MM-dd HH-mm-ss " ); -- 設定屬性信息
xmlNote.Attributes.Append(xmlattr); ---- 在book節點上新增一個屬性
// 新增另一個屬性,要找出本xml文檔中的最大號,進行比較
XmlElement xmld = xmlDoc.DocumentElement; ---- 聲明一元素
string strmax = "" ;
int count_elem = 0 ;
count_elem = xmld.ChildNodes.Count - 2 ; ----- 找到最大節點的index
XmlElement nodeee = (XmlElement)xmld.ChildNodes[count_elem];──找到此節點並轉類型
strmax = nodeee.GetAttribute( " Staff_id " ); --- 取到想要的信息
xmlattr = xmlDoc.CreateAttribute( " Staff_id " ); ----- 增加些元素
xmlattr.Value = strmax.Substring( 0 , 5 ) + ( int .Parse(strmax.Substring( 5 , 1 )) + 1 ) ;
xmlNote.Attributes.Append(xmlattr);
// 增加一個型式的節點
childnote = xmlNote.AppendChild(xmlDoc.CreateElement( " adress " ));
childnote.InnerText = stradress;
// add skill----增加另一個樣式的節點,此節點又有子節點
childnote = xmlNote.AppendChild(xmlDoc.CreateElement( " P_Skills " ));
for ( int i = 0 ;i < 3 ;i ++ ) {
XmlNode childsnote=null;
childsnote=childnote.AppendChild(xmlDoc.CreateElement("P_Skill" & (i+1)));
childsnote.InnerText="java"+i;
}
// 增加有data樣式的節點,如<message><![CDATA[xxxxxxxx]]> </message>
childnote = xmlNote.AppendChild(xmlDoc.CreateElement( " message " ));
childnote.AppendChild(xmlDoc.CreateCDataSection(strmessage));
// 保存新增後的文件
xmlDoc.Save(Server.MapPath( " book.xml " ));
第二種方法:
XmlDocument xmlDoc
=
new
XmlDocument();
xmlDoc.Load(Server.MapPath( " users.xml " ));
XmlNode xmlRoot = xmlDoc.DocumentElement;
XmlElement xmlUser = xmlDoc.CreateElement( " User " );
XmlElement xmlName = xmlDoc.CreateElement( " Name " );
XmlElement xmlEmail = xmlDoc.CreateElement( " Email " );
XmlElement xmlPassword = xmlDoc.CreateElement( " Password " );
xmlName.InnerText = " addName " ;
xmlEmail.InnerText = " addEmail " ;
xmlPassword.InnerText = " addPassword " ;
xmlUser.AppendChild(xmlName);
xmlUser.AppendChild(xmlEmail);
xmlUser.AppendChild(xmlPassword);
XmlAttribute xmlattr = xmlDoc.CreateAttribute( " newAttr " );
xmlattr.Value = " AttributeValue " ;
xmlDoc.DocumentElement.SetAttributeNode(xmlattr);
xmlRoot.AppendChild(xmlUser);
xmlDoc.Load(Server.MapPath( " users.xml " ));
XmlNode xmlRoot = xmlDoc.DocumentElement;
XmlElement xmlUser = xmlDoc.CreateElement( " User " );
XmlElement xmlName = xmlDoc.CreateElement( " Name " );
XmlElement xmlEmail = xmlDoc.CreateElement( " Email " );
XmlElement xmlPassword = xmlDoc.CreateElement( " Password " );
xmlName.InnerText = " addName " ;
xmlEmail.InnerText = " addEmail " ;
xmlPassword.InnerText = " addPassword " ;
xmlUser.AppendChild(xmlName);
xmlUser.AppendChild(xmlEmail);
xmlUser.AppendChild(xmlPassword);
XmlAttribute xmlattr = xmlDoc.CreateAttribute( " newAttr " );
xmlattr.Value = " AttributeValue " ;
xmlDoc.DocumentElement.SetAttributeNode(xmlattr);
xmlRoot.AppendChild(xmlUser);
4、 修改一畢資料,在server端,操作如下:
XmlDocument xmlDoc
=
new
XmlDocument();
xmlDoc.Load(Server.MapPath( " book.xml " ));
string xpth_str = "" ;
xpth_str = " /guestbook/book[@Staff_id=' " + this .txtCNO_ID.Text + " '] " ;
System.Xml.XmlElement xmlet = xmlDoc.DocumentElement;
System.Xml.XmlNode xmnode = xmlet.SelectSingleNode(xpth_str); ------- 取得有條件相關節點
xmnode.ChildNodes[ 4 ].ChildNodes[ 0 ].InnerText = xmnode.ChildNodes[ 4 ].ChildNodes[ 0 ].InnerText.ToString() + " 66 " ;
xmnode.ChildNodes[ 4 ].ChildNodes[ 1 ].InnerText = xmnode.ChildNodes[ 4 ].ChildNodes[ 1 ].InnerText.ToString() + " 66 " ;
xmnode.ChildNodes[ 4 ].ChildNodes[ 2 ].InnerText = xmnode.ChildNodes[ 4 ].ChildNodes[ 2 ].InnerText.ToString() + " 66 " ;
/**/ ///以上為讀取有子節點的節點相關資料並進修改
string strmess = xmnode.ChildNodes[ 5 ].InnerText.ToString();
System.Xml.XmlNode mess = xmnode.SelectSingleNode( " message " );
mess.RemoveAll();
mess.AppendChild(xmlDoc.CreateCDataSection(strmess + " _upd " ));
// 修改data資料
xmlDoc.Save(Server.MapPath( " book.xml " ));
xmlDoc.Load(Server.MapPath( " book.xml " ));
string xpth_str = "" ;
xpth_str = " /guestbook/book[@Staff_id=' " + this .txtCNO_ID.Text + " '] " ;
System.Xml.XmlElement xmlet = xmlDoc.DocumentElement;
System.Xml.XmlNode xmnode = xmlet.SelectSingleNode(xpth_str); ------- 取得有條件相關節點
xmnode.ChildNodes[ 4 ].ChildNodes[ 0 ].InnerText = xmnode.ChildNodes[ 4 ].ChildNodes[ 0 ].InnerText.ToString() + " 66 " ;
xmnode.ChildNodes[ 4 ].ChildNodes[ 1 ].InnerText = xmnode.ChildNodes[ 4 ].ChildNodes[ 1 ].InnerText.ToString() + " 66 " ;
xmnode.ChildNodes[ 4 ].ChildNodes[ 2 ].InnerText = xmnode.ChildNodes[ 4 ].ChildNodes[ 2 ].InnerText.ToString() + " 66 " ;
/**/ ///以上為讀取有子節點的節點相關資料並進修改
string strmess = xmnode.ChildNodes[ 5 ].InnerText.ToString();
System.Xml.XmlNode mess = xmnode.SelectSingleNode( " message " );
mess.RemoveAll();
mess.AppendChild(xmlDoc.CreateCDataSection(strmess + " _upd " ));
// 修改data資料
xmlDoc.Save(Server.MapPath( " book.xml " ));
5、 刪除指定節點,操作如下:(在server端)
XmlDocument xmlDoc
=
new
XmlDocument();
xmlDoc.Load(Server.MapPath( " book.xml " ));
string xpth_str = "" ;
xpth_str = " /guestbook/book[@Staff_id=' " + this .txtCNO_ID.Text + " '] " ;
System.Xml.XmlElement xmlet = xmlDoc.DocumentElement;
xmlet.RemoveChild(xmlet.SelectSingleNode(xpth_str));
xmlDoc.Save(Server.MapPath( " book.xml " ));
xmlDoc.Load(Server.MapPath( " book.xml " ));
string xpth_str = "" ;
xpth_str = " /guestbook/book[@Staff_id=' " + this .txtCNO_ID.Text + " '] " ;
System.Xml.XmlElement xmlet = xmlDoc.DocumentElement;
xmlet.RemoveChild(xmlet.SelectSingleNode(xpth_str));
xmlDoc.Save(Server.MapPath( " book.xml " ));
第二部分
───xsl的語法運用
1、 xml的語法結構:
<?
xml version="1.0" encoding="utf-8"
?>
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
如下是一個本測試中的example:
< guestbook >
< book date ="2006-01-05 18:10:00" Staff_id ="c03031" >
< name > Susan </ name >
< email > snowonyou@eyou.com </ email >
< homepage > http://www.hao123.com </ homepage >
< adress > 河南許昌 </ adress >
< P_Skills >
< P_Skill1 > ASP </ P_Skill1 >
< P_Skill2 > ASP.NET </ P_Skill2 >
< P_Skill3 > SQL </ P_Skill3 >
</ P_Skills >
< message > <! [CDATA[這是我第2個用xml做的例子,學習學習,加油!!不認輸!_upd]] > </ message >
</ book >
</ guestbook >
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
如下是一個本測試中的example:
< guestbook >
< book date ="2006-01-05 18:10:00" Staff_id ="c03031" >
< name > Susan </ name >
< email > snowonyou@eyou.com </ email >
< homepage > http://www.hao123.com </ homepage >
< adress > 河南許昌 </ adress >
< P_Skills >
< P_Skill1 > ASP </ P_Skill1 >
< P_Skill2 > ASP.NET </ P_Skill2 >
< P_Skill3 > SQL </ P_Skill3 >
</ P_Skills >
< message > <! [CDATA[這是我第2個用xml做的例子,學習學習,加油!!不認輸!_upd]] > </ message >
</ book >
</ guestbook >
2、 xsl 的語法結權
<?
xml version="1.0" encoding="utf-8"
?>
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
< xsl:template match ="guestbook" >
,,,,,,,對相關資料進行操作
</ xsl:template >
</ xsl:stylesheet >
下面是一個例子:
< xsl:template match ="guestbook" >
< html >
< body bgcolor ="#CCCC66" onselectstart ="return false" >
< center >< h1 > 留言版 </ h1 ></ center >
< table border ="1" width ="100%" bgcolor ="#bbCCCC" >
< tr bgcolor ="#0099CC" >
< td > 最新留言|| < a href ="../index.html" > 我要留言 </ a ></ td >
</ tr >
< tr >
< table width ="100%" border ="1" >
< xsl:for-each select ="book" >
< xsl:sort select ="@date" ></ xsl:sort >
< tr bgcolor ="#bbCC00" >
< td rowspan ="2" width ="20%" >
學號: < xsl:value-of select ="substring(@Staff_id, 1, 3)" ></ xsl:value-of >
留言人: < xsl:value-of select ="name" ></ xsl:value-of >< p />
來自: < xsl:value-of select ="adress" ></ xsl:value-of >< p />
技能: < xsl:value-of select ="P_Skills/P_Skill1" /> 、
< xsl:value-of select ="P_Skills/P_Skill2" /> 、
< xsl:value-of select ="P_Skills/P_Skill3" />
</ td >
< td width ="80%" >
留言時間: < xsl:value-of select ="@date" ></ xsl:value-of > ||
Email: < xsl:value-of select ="email" ></ xsl:value-of > ||
個人主頁: < xsl:value-of select ="homepage" ></ xsl:value-of >
</ td >
</ tr >
< tr >
< xsl:attribute name ="ONCLICK" > alert("呵呵,原來是這樣呀"); </ xsl:attribute >
< xsl:attribute name ="oncontextmenu" >
window.event.cancelBubble _u32 ?true;check_log();loadContextMenu('context.xml',
' < xsl:value-of select ="@Staff_id" ></ xsl:value-of > ',
' < xsl:value-of select ="name" ></ xsl:value-of > ');return false;
</ xsl:attribute >
< td width ="72%" >
< xsl:choose >
< xsl:when test ="position()=last()" >
< a >< xsl:attribute name ="title" >
這是最後一畢
</ xsl:attribute >
< pre >< xsl:value-of select ="message" ></ xsl:value-of ></ pre >
</ a >
</ xsl:when >
< xsl:otherwise >
< pre >< xsl:value-of select ="message" ></ xsl:value-of ></ pre >
</ xsl:otherwise >
</ xsl:choose >
</ td >
</ tr >
</ xsl:for-each >
</ table >
</ tr >
</ table >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
< xsl:template match ="guestbook" >
,,,,,,,對相關資料進行操作
</ xsl:template >
</ xsl:stylesheet >
下面是一個例子:
< xsl:template match ="guestbook" >
< html >
< body bgcolor ="#CCCC66" onselectstart ="return false" >
< center >< h1 > 留言版 </ h1 ></ center >
< table border ="1" width ="100%" bgcolor ="#bbCCCC" >
< tr bgcolor ="#0099CC" >
< td > 最新留言|| < a href ="../index.html" > 我要留言 </ a ></ td >
</ tr >
< tr >
< table width ="100%" border ="1" >
< xsl:for-each select ="book" >
< xsl:sort select ="@date" ></ xsl:sort >
< tr bgcolor ="#bbCC00" >
< td rowspan ="2" width ="20%" >
學號: < xsl:value-of select ="substring(@Staff_id, 1, 3)" ></ xsl:value-of >
留言人: < xsl:value-of select ="name" ></ xsl:value-of >< p />
來自: < xsl:value-of select ="adress" ></ xsl:value-of >< p />
技能: < xsl:value-of select ="P_Skills/P_Skill1" /> 、
< xsl:value-of select ="P_Skills/P_Skill2" /> 、
< xsl:value-of select ="P_Skills/P_Skill3" />
</ td >
< td width ="80%" >
留言時間: < xsl:value-of select ="@date" ></ xsl:value-of > ||
Email: < xsl:value-of select ="email" ></ xsl:value-of > ||
個人主頁: < xsl:value-of select ="homepage" ></ xsl:value-of >
</ td >
</ tr >
< tr >
< xsl:attribute name ="ONCLICK" > alert("呵呵,原來是這樣呀"); </ xsl:attribute >
< xsl:attribute name ="oncontextmenu" >
window.event.cancelBubble _u32 ?true;check_log();loadContextMenu('context.xml',
' < xsl:value-of select ="@Staff_id" ></ xsl:value-of > ',
' < xsl:value-of select ="name" ></ xsl:value-of > ');return false;
</ xsl:attribute >
< td width ="72%" >
< xsl:choose >
< xsl:when test ="position()=last()" >
< a >< xsl:attribute name ="title" >
這是最後一畢
</ xsl:attribute >
< pre >< xsl:value-of select ="message" ></ xsl:value-of ></ pre >
</ a >
</ xsl:when >
< xsl:otherwise >
< pre >< xsl:value-of select ="message" ></ xsl:value-of ></ pre >
</ xsl:otherwise >
</ xsl:choose >
</ td >
</ tr >
</ xsl:for-each >
</ table >
</ tr >
</ table >
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >