Browse DevX
Sign up for e-mail newsletters from DevX


Well-structured XML Goes Cosmopolitan : Page 2

Everyone knows that among its many uses, XML can store the same data in multiple languages and dish it out like a U.N. translator when the computers of the world come calling. What everyone doesn't know is how best to structure that polyglot of data. There's more than one way




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Language-oriented Structure
Listing 3 is the same resume example but this time structured in the language-oriented way.

Listing 3. Language-oriented structure for a multilingual resume.

<?xml version="1.0" encoding="UTF-8"?> <cv> <language xml:lang="en"> <title>Curriculum Vitae</title> <skills> <title>Skills</title> <skill>Project management</skill> </skills> <education> <title>Education</title> <school>University of Technology</school> <graduation>1993</graduation> </education> </language> <language xml:lang="fi"> <title>Ansioluettelo</title> <skills> <title>Taidot</title> <skill>Projektinhallinta</skill> </skills> <education> <title>Koulutus</title> <school>Teknillinen korkeakoulu</school> <graduation>1993</graduation> </education> </language> <language xml:lang="ja"> <title>履歴書</title> <skills> <title>技術</title> <skill>プロジェクト管理</skill> </skills> <education> <title>教育</title> <school>技術の大学</school> <graduation>1993</graduation> </education> </language> </cv>

This XML document in Listing 3 has a fundamentally different structure than the one in Listing 1. Listing 3 introduces a "language" element at the top level in the document structure. This splits the document into high-level sections—one for each language the author intends to support. Each part uses an identical copy of the document structure, substituting the appropriate words for that language. Note also that the xml:lang attribute is used only within "language" elements. There is no need to scatter these attributes throughout the document.

Listing 4 is an XSL stylesheet for localized XHTML transformation.

Listing 4: Transforming the language-oriented structure for a multilingual resume.

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:param name="currLang">en</xsl:param> <xsl:output method="xml" encoding="UTF-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/> <xsl:template match="cv"> <xsl:apply-templates select="language[lang($currLang)]"/> </xsl:template> <xsl:template match="language"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{$currLang}" lang="{$currLang}"> <head> <title><xsl:value-of select="title"/></title> <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> </head> <body> <h2><xsl:value-of select="title"/></h2> <xsl:apply-templates select="skills"/> <xsl:apply-templates select="education"/> </body> </html> </xsl:template> <xsl:template match="skills"> <h3><xsl:value-of select="title"/></h3> <ul> <xsl:apply-templates select="skill"/> </ul> </xsl:template> <xsl:template match="skill"> <li><xsl:value-of select="."/></li> </xsl:template> <xsl:template match="education"> <h3><xsl:value-of select="title"/></h3> <p> <xsl:value-of select="school"/> <xsl:text> </xsl:text> <xsl:value-of select="graduation"/> </p> </xsl:template> </xsl:stylesheet>

The major difference between this stylesheet and the one in Listing 2 is in the first template, the one that processes the "cv" element. In Listing 4 the template selects the preferred language subtree and processes the elements of that subtree only. The rest of the templates do not need to worry about the current language because the choice has already been made. The stylesheet is therefore much more straightforward than the one in Listing 2.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date