Record your recent accumulation (export XSL + XML and JS operations on XML)

Source: Internet
Author: User
Tags xsl

Implementation requirements: In the early stage of work, the Silverlight Workflow Editor should interact with the Web service layer information. Of course, you may not pass the web page, but you must set up a communication service channel, it can be implemented in V2.0.

On the Silverlight and web pages, parse and encapsulate XML based on the XML Information Carrier, and parse and encapsulate XML based on JS on the web pages, because the XML format is

<?xml version="1.0" encoding="utf-8"?>
<ParticipatorInfo>
<RoleList>
<RoleInfo ID='1' Name='234' />
<RoleInfo ID='2' Name='234' />
<RoleInfo ID='3' Name='234' />
<RoleInfo ID='4' Name='234' />
</RoleList>
<DeptList>
<DeptInfo ID='5' Name='234234' />
<DeptInfo ID='6' Name='234234' />
</DeptList>
<PostList>
<PostInfo ID='7' Name='234234234' />
</PostList>
<UserList>
<UserInfo ID='8' Name='23242432' />
</UserList>
</ParticipatorInfo>

This information needs to be displayed on the web page. solution 1 is to bind XML as the data source in the background, and solution 2 is to bind it to the table on the web page in the form of data island,

Considering the operations on the page, binding, and display separation, select the second solution, which ensures the independence of XML. You can add and delete information to operate XML objects through Js.

However, table binding to the XML data island requires the XML format. The above XML does not meet the requirements. I do not know what a better solution is.

When formatting and transforming XML, you can use XSL to format it. Create an XSL to display XML data as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<! -- File name: XSL -->
<XSL: template match = "/">
<Table border = "1" bgcolor = "# 4eb7de" align = "center">
<Tr>
<TH> type </Th>
<TH> participants </Th>
</Tr>
<XSL: Apply-templates select = "// roleinfo"/>
<XSL: Apply-templates select = "// deptinfo"/>
<XSL: Apply-templates select = "// postinfo"/>
<XSL: Apply-templates select = "// userinfo"/>
</Table>
</XSL: Template>
<XSL: template match = "// roleinfo">
<Tr>
<TD> role </TD>
<TD>
<XSL: value-of select = "@ name"/>
</TD>
</Tr>
</XSL: Template>
<XSL: template match = "// deptinfo">
<Tr>
<TD> organizational structure </TD>
<TD>
<XSL: value-of select = "@ name"/>
</TD>
</Tr>
</XSL: Template>
<XSL: template match = "// postinfo">
<Tr>
<TD> position </TD>
<TD>
<XSL: value-of select = "@ name"/>
</TD>
</Tr>
</XSL: Template>
<XSL: template match = "// userinfo">
<Tr>
<TD> User </TD>
<TD>
<XSL: value-of select = "@ name"/>
</TD>
</Tr>
</XSL: Template>
</XSL: stylesheet>

Then, the XML is output in the XSL format using the JS script.

The XML string function on the siverlight vector page is defined on the Web page to convert the XSL function. The output function code is as follows:

// Load the XML from the flow Editor
Function loadxml (){
VaR xmlstr = Window. dialogarguments. sendpart ();
Xmldom = createxml (xmlstr); // converts an XML string to an XML Object
Writediv (); // output the XML object to the DIV
}
// Format the XML Object content into the output Div
Function writediv (){
Document. getelementbyid ('divdataare'). innerhtml = transxmlbyxsl (xmldom); // convert XML objects in the XSL format
}
// Convert string (XML) in the process editor to an XML Object
Function createxml (STR ){
VaR XML;
If (document. All ){
Xml = new activexobject ("Microsoft. xmldom ");
XML. loadxml (STR );
}
Else {
Xml = new domparser (). parsefromstring (STR, "text/XML ");
}
XML. async = false
Return XML;
}
// Output formatted XML Object
Function transxmlbyxsl (DOM ){
VaR XSL = new activexobject ("Microsoft. xmldom ")
XSL. async = false
XSL. Load ("maid. XSL ")
Return Dom. transformnode (XSL)
}

At this point, the XML Conversion XSL format output is complete.

After adding, deleting, and modifying the XML operation on the data source, reset the wirtediv () function output. The Code is as follows:

// Remove the node of the XML Object
Function removenode (type, ID ){
VaR parentnode = type;
VaR pnode = xmldom. selectsinglenode ("//" + parentnode );
VaR snodes = xmldom. selectnodes ("//" + parentnode. Replace ("list", "info "));
For (VAR I = 0; I <snodes. length; I ++ ){
If (snodes [I]. attributes [0]. value = ID ){
Pnode. removechild (snodes [I]);
Break;
}
}
Writediv ();
}
// Add the node of the XML Object
Function addnode (type, name, ID ){
If (ID = '_ 0 ')
Return;
VaR parentnode = type;
VaR pnode = xmldom. selectsinglenode ("//" + parentnode); // Father's Day
VaR snodes = xmldom. selectnodes ("//" + parentnode. Replace ("list", "info "));
For (VAR I = 0; I <snodes. length; I ++ ){
If (snodes [I]. attributes [0]. value = ID)
Return;
}
VaR snode = xmldom. createelement (parentnode. Replace ("list", "info"); // create a byte
VaR attrid = xmldom. createattribute ("ID ");
Attrid. value = ID;
Snode. setattributenode (attrid );
VaR attrname = xmldom. createattribute ("name ");
Attrname. value = Name;
Snode. setattributenode (attrname );
Pnode. appendchild (snode );
Writediv ();
}

This is the end of my work. If any of you are interested, leave a message to contact me.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.