SoFunction
Updated on 2025-04-07

Combination of XML and HTML (Part 2)

Nested processing of XML

Generally speaking, the result set we get from the database may be large, so when we return from the server to the client, the data will be divided into several pages for transmission. At this time, the DATAPAGESIZE attribute in the TABLE element can be used to specify the number of recordset entries for each page.

For example:

<TABLE DATASRC=“#xmldso” DATAPAGESIZE=10>

Obviously, if the XML data format is symmetric, it will work well whether it is mapped to the ADO record set or bound to a table element. In practical applications, there are many examples of XML data being asymmetric. For example, there may be more than one author of a book, which will cause some trouble when mapping and binding. The solution to the problem is to use nesting. Each row of table still corresponds to a main element, and each column also corresponds to a child element. For duplicate elements, a nested table is used. Let's assume that in the first book, the author of Dean Straight, and the author of the second book, Charlotte Cooper, Shelley Burke, and Regina Murphy. At this time, the binding process is as follows:

● Create a TABLE element and assign the data island ID to the DATAFLD attribute;

● For individual XML elements, such as <isbn>, create a TD element and set the corresponding DATAFLD attribute;

● For repeating elements, nest a table inside the TD element;

● Display author information in the form of a single row and a single column.

Note that the DATAFLD attribute here must be set to "$TEXT".

To ensure that all the contents of the nested elements are displayed in the specified elements.

The complete HTML code looks like this:

<TABLE BORDER=1 DATASRC=“#xmldso”>

<THEAD><TR><TH>Title</TH>

<TH>ISBN</TH>

<TH>Author</TH></TR></THEAD>

<TBODY>

<TR><TD>

<DIV DATAFLD=“title”></DIV></TD>

<TD><DIV DATAFLD=“isbn”>

</DIV></TD>

<TD>

<TABLE BORDER=0 DATASRC=“#xmldso” DATAFLD=“author”>

<TR><TD><SPAN DATAFLD=“$Text”></SPAN></TD></TR>

</TABLE>

</TD>

</TR></TBODY>

</TABLE>

In fact, the best case of using DSO is for structurally symmetric data, while the more effective way to process asymmetric data is to use the DOM technology we will introduce in the future.

Application of DSO technology

1. Accessing attributes of elements

It is very simple to access the attributes of an element using DSO, and you can directly process the attributes as child elements.

For example:

<book isbn=“9-001-122-12”>

……

</book>

In this way, when bound to an HTML table, you can directly process it as child elements:

<TD><SPAN DATAFLD=“isbn”> </SPAN></TD>

If you encounter the same attribute name as the child element name, add "!" before the element name to distinguish it. 2. Traversal record set

One of the benefits of DSO processing XML data islands as ADO recordsets is that they can access data sources using various methods provided by ADO, especially when binding data islands to HTML elements such as SPAN, DIV and INPUT. Usually these elements display the first record of the record set. To traverse and browse the record set, you can use ADO methods: Move, MoveFirst, MoveLast, MoveNext and MovePrevious. For example, to create a button response function, as long as the user clicks the "Next" button, he can browse the corresponding records one by one.

For example:

<XML ID=“xmldso” SRC=“”>

</XML>

Sub btnNext_onclick()

End Sub

3. Combined with Script language

Some users are more accustomed to writing Script languages, and using DSO technology can also be well combined with various Scripts.

For example (taking VB Script as an example), when accessing a record set, the code is as follows:

Dim rsBooks

Set rsBooks =

Access the value of a field (child element):

Dim sTitle

sTitle = rsBooks(“title”)

You can use innerText and innerHTML attributes to pass the resulting value to the HTML element. For example, there is a DIV element named divTitle, and the assignment code looks like this:

= sTitle

The scripting program can also handle many DSO events. The following table lists some of them:

The method to handle various events in a script is to use the FOR attribute to specify the XML data island ID in the <SCRIPT> tag and use the EVENT attribute to determine the event type.

For example, get the number of entries in a record set:

<SCRIPT Language=“VB Script” FOR=“xmldso” EVENT=“onDataAvailable”>

=

</SCRIPT>

In addition to displaying recorded data, the script program can also quickly query, sort, edit and other operations on the record set. But it should be pointed out that although ADO technology provides methods such as SortColumn and SortAscending to sort XML data, the effect is not as good as the sorting operations in XSL, so it is recommended that you make full use of XSL technology to achieve this part of the function.

The remaining functions, such as using scripts to perform operations such as adding, deleting, modifying recordsets, or paginating HTML tables, etc., will not be explained here, and the usage is similar to the previous operations. Finally, it should be noted that all operations on DSO objects are completed on the user side, and are actually a copy of the server data object. The advantage of this is that it avoids the network from bearing the burden of large amounts of data communication. However, any operations done by the user at this time have no effect on the data stored on the server. If you want to modify the server records, you need to use the data exchange technology between the client and the server. We will introduce this in the future.