|
||
| Inside Technique : Building a Content Server with XML and ASP : Editing Files Our story editor allows content to be added, edited, and deleted. Every time a story
is manipulated, the index page needs to be updated. We perform the updates using
the XML document object model. The XML document object model provides a complete
representation of the XML document tree. To manipulate the XML document, you need
to traverse or query the tree for a specific node. For example, looking
back at our index.xml file:
The storyindex element is the root node of our tree. It currently contains a child node list (a collection of nodes) consisting of the two article elements. Each article has a child node list consisting of an id, title, and author. To manipulate the document, you need to traverse this hierarchy. You can either traverse this hierarchy manually analyzing each node's children or you can use the DOM to query for a node. Stories are edited based on the ID. When a story is going to be edited, we need to locate the appropriate article in the index.xml file based on this ID. We can either do this by walking the entire document looking for the ID or we can use the selectSingleNode method to query the document. The latter method efficiently accomplishes in one statement the same result as manually writing many lines of complex looping (and many times recursive) code. The selectSingleNode method takes an XSL pattern and returns the matching node. The pattern language
enables queries to be executed against the XML document. When a story is edited, we
execute a query that returns the article node containing the story with specified ID.
For example, if the first story is edited (eg, id=1) we query the index as follows:
This query looks inside of the storyindex element for an article element that contains an id element with the content of "1". The brackets "[]" around the ID element specifies the criteria that must be matched to return the article node. The "." is used to match the contents of the ID element to a value (in this case "1"). The pattern matching syntax is extremely powerful and will be the topic of a future article. In the meantime, you can find a pattern matching reference at Microsoft's XML Web-Site. We use the pattern matching syntax from the XML DOM as follows:
This retrieves an existing story and stores the values in three variables: sTitle,
sAuthor, and sStory. Inserting these values into the form
is elementary ASP scripting:
This demonstrates how the articles are retrieved for editing. Next we discuss what happens when the user saves or deletes the article. Page 1:Building a Content Server with XML and ASP © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |