📜 ⬆️ ⬇️

Visualization of xml documents

I think for many it is no secret that xml is a fairly convenient way to store and transfer information. For some time now I come across in the literature for the statement that in the future xml should replace html, and the appearance of xhtml is a confirmation of this. But there is a nuance. All browsers know how to display html documents, but none of them know how to display xml documents. This is primarily due to the fact that when creating an xml document you can enter your tags. I want to briefly (this is a very big topic, in fact) tell about the so-called "Conversion tables of xml-documents."
I think that many probably are familiar with them, but I hope that this may be useful to someone, and, considering my love for practice, I want to show everything with a very simple example.


What we have


Consider a simple xml document:
  1. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  2. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  3. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  4. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  5. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  6. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  7. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  8. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  9. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  10. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  11. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  12. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  13. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  14. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  15. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  16. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  17. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  18. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  19. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  20. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  21. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>
  22. <?xml version="1.0" encoding="utf-8"?> <document> <header> Xml-</header> <items> <item id="1"> <name>Habrahabr.ru</name> <description> . . </description> </item> <item id="2"> <name>Bash.org.ru</name> <description> . , , . </description> </item> </items> </document>

If we try to view it in a browser, we will see something like this:


There are several ways to visualize the contents of this document.
You can connect a regular style sheet (css-file) or a conversion table to it.
The conversion table is an xml-document, designed according to certain rules and having the extension .xslt.
')
Suppose we need to display the information contained in the xml-document, for this we create a conversion table (the file will be called style.xslt):
  1. <? xml version = "1.0" encoding = "utf-8"?>
  2. <xsl: stylesheet version = "1.0"
  3. xmlns: xsl = "http://www.w3.org/1999/XSL/Transform"
  4. xmlns: msxsl = "urn: schemas-microsoft-com: xslt"
  5. exclude-result-prefixes = "msxsl">
  6. <xsl: template match = "document">
  7. <html>
  8. <head> <title> <xsl: value-of select = "header" /> </ title> </ head>
  9. <body> <xsl: apply-templates select = "items" /> </ body>
  10. </ html>
  11. </ xsl: template>
  12. <xsl: template match = "items">
  13. <ul> <xsl: apply-templates select = "item" /> </ ul>
  14. </ xsl: template>
  15. <xsl: template match = "item">
  16. <li> <a>
  17. <xsl: attribute name = "href">
  18. http: // <xsl: value-of select = "name" />
  19. </ xsl: attribute>
  20. <xsl: value-of select = "name" />
  21. </a> - <xsl: value-of select = "description" />
  22. <li> </ xsl: template>
  23. </ xsl: stylesheet>


Connect the conversion table (to the original xml-document of course) as follows:
<? xml version = "1.0" encoding = "utf-8"?>
<? xml-stylesheet type = "text / xsl" href = "style.xslt"?>
As a result, opening the same file in the browser we will see the following:



In a nutshell, how it works


There are three templates in the conversion table: document, items, and item. When an xsl: apply-templates element is found in a template, it replaces what happens as a result of processing all the elements that satisfy the select expression (I just indicated the names of the tags, although there may be quite complex constructions). Similarly, tag values ​​(xsl: value-of tag) and attributes for tags (xsl: attribute tag) are inserted.

The document template forms the basis of the html page (html tags, head, body), and they substitute the page title and the list of elements formed by the items template (the list items are formed by the items template).

All conversion is done on the client side.

This is only a small fraction of what conversion technology can provide. Visual Studio has a convenient interface for working with them, including viewing the generated html.

Source: https://habr.com/ru/post/43356/


All Articles