XML Plus ASP to implement Web page "localization" _xml/rss

Source: Internet
Author: User
Tags character set chr html form xml parser xpath xpath code
Want your site to be seen by surfers from many countries? There is no doubt that this requires the provision of multiple language versions of the page content, which is called
The localization feature. The easiest way to think about it is to write the page content of multiple languages separately, then place it in a different directory, and then add navigation on the homepage.
Links to the respective language locations. Obviously, such a process would waste a lot of space containing common elements and design frameworks, and it would be annoying to modify and maintain.
Zoar Is there any good way to do it? Of course, this article describes how to use XML and ASP technology to achieve this goal.
This article discusses the following areas: Creating XML documents that contain language resources, creating Web page templates using ASP, using XPath syntax and MSXML3
The parser locates the target content in an XML document and dynamically inserts language strings into the HTML stream.
Preparation conditions
The techniques discussed in this article relate to the following: IIS4 or PWS (including ASP features), MSXML version 3.
Function Show
The routines to be discussed in this article are a simple HTML form for submitting name and address information. Here is the Spanish in Netscape Navigator
Illustration of the statement:
Using the techniques described in this article, you can easily add multiple languages to your site by updating an XML file.
Create an XML document that contains language resources
First, use the Favorite page editor to create a Web page file called xmltrans.asp. I found the use of static placeholder text (such as "first
Name ") is easiest to design the page. When XML is ready, you can replace these static text with variables. To download the routine file, click here.
When you have finished designing the basic page, start creating a well-formed XML document that contains the language strings. Here, I use a plain text editor
Notepad writes the initial set of XML languages-the English version. Notepad makes me feel closer to the source code. The XML file is named Xmltrans.xml. The following list
Contains the XML file code content for Chinese, French, and Spanish strings:
<?xml version= "1.0" encoding= "UTF-8"?>
<languages>
<language xml:lang= "en" engname= "中文版" langname= "中文版" charset= "Windows-1252" >
<title>localize ASP with xml</title>
<firstname>first name</firstname>
<lastname>last name</lastname>
<prefix>Prefix</prefix>
<suffix>Suffix</suffix>
<address1>Address</address1>
<address2>address 2</address2>
<address3>address 3</address3>
<city>City</city>
<region>State/Province</region>
<postalcode>postal code</postalcode>
<areacode>area code</areacode>
<telephone>telephone number</telephone>
<submit>OK</submit>
<lang>en</lang>
<charset>Windows-1252</charset>
<langname>English</langname>
</language>
<language xml:lang= "fr" engname= "French" langname= "Franζais" charset= "Windows-1252" >
<title>localize ASP with xml-french</title>
<firstname>Prénom</firstname>
<lastname>Nom</lastname>
<prefix>préfixe (M., Mme, Mlle) </prefix>
<suffix>Suffixe/Titre</suffix>
<address1>Rue</address1>
<address2/>
<address3/>
<city>Ville</city>
<region>Région/Province</region>
<postalcode>code postal</postalcode>
<areacode>indicatif régional</areacode>
<telephone>numéro de téléphone</telephone>
<submit>OK</submit>
<lang>fr</lang>
<charset>Windows-1252</charset>
<langname>French</langname>
</language>
<language xml:lang= "sp" engname= "Spanish" langname= "Espanol" charset= "Windows-1252" >
<title>localize ASP with xml-spanish</title>
<firstname>Nombre</firstname>
<lastname>apellido paterno</lastname>
<prefix>prefijo (Sr., Sra., Srta.) </prefix>
<suffix>sufijo o título</suffix>
<address1>línea 1 de dirección</address1>
<address2/>
<address3/>
<city>Ciudad</city>
<region>región, Estado o provincia</region>
<postalcode>código postal</postalcode>
<areacode>código deárea</areacode>
<telephone>número de Teléfono </telephone>
<submit>OK</submit>
<lang>sp</lang>
<charset>iso-8859-1</charset>
<langname>Spanish</langname>
</language>
</languages>
The first line of the Xmltrans.xml file is the XML declaration. The Version property tells the reader that the document conforms to the XML 1.0 standard, and the encoding attribute indicates parsing
Use compressed version of Unicode:
<?xml version= "1.0" encoding= "UTF-8"?>
An XML document requires a root element that contains other elements. Because the routines here contain some languages, the name root element is languages:
<languages>
As a collection type, the languages element contains one or more language elements:
<language xml:lang= "en" engname= "中文版" langname= "中文版" charset= "Windows-1252" >
The above language tag contains 4 properties. The Xml:lang property is one of the most important, in the ASP program will be through this property value of the 2-letter language code Search
String Group. Other attributes, such as CharSet, can be used for future extensions.
In each language node, I have added an element as a variable that contains the text that is displayed on the HTML page. XML allows meaningful names to be used as
Custom tags, for example, I use the <title> element to include the title of an HTML page. The same,<firstname> element contains the string "name".
<title>localize ASP with xml</title>
<firstname>first name</firstname>
Finally, a well-formed XML document contains a complete set of strings corresponding to the HTML page. The following illustration shows the markup and Chinese language characters in an XML file
String. Note that the Chinese version is now discussed here and more languages can be added later.

ASP File Code Analysis
Now come back and look at the ASP file xmltrans.asp. In which we use the XML Path Language (XPath), which can be used in Microsoft's XML parser (MSXML)
Perform. You can think of XPath as a tool to arrive at a predetermined location of content in an XML document, which is similar to entering a path to execute a file on the command line.
such as C:\Winnt\Notepad.exe, or in the browser typed a URL to access the page. The following study of this ASP file to see how to retrieve the Chinese language
Character string. First, we instantiate an XML parser work object called Msxml2.domdocument:
Set Doc=server._
CreateObject _
("MSXML2.") DOMDocument ")
Since the ASP program cannot handle events like the WIN32 program, here we turn off the asynchronous Operation option. That makes sure you're moving to another task.
, the script will wait until the current event completes:
Doc.async = False
The XML document containing the string is then loaded using the Load method of the DOMDocument object. If an error occurs during the reprint, send a warning message and stop
Stop running:
If not doc. Load (server._
MapPath ("Xmltrans.xml")) Then
Msg= "Failed to load"
Msg=msg & "The XML file"
Response.Write msg
Response.End
End If
We can query XML documents using two methods: XSL and XPath. Here let the parser use the latter, passing the property name and the correct value to the
SetProperty method:
Doc.setproperty _
"SelectionLanguage", "XPath"
Then, create a path that selects portions of the XML document. Obviously, the related string is located somewhere in the languages element, so languages becomes
The first part of the path. At the same time we know that strings are contained within a language element, but which one is it? Please don't forget in the previous XML document
, we embed a property called Xml:lang and give a unique numeric "en". So that's what we're going to choose, and here's the syntax for manipulating it:
Sel= "/languages/language"
Sel=sel & "[@xml: lang= ' en ']"
This is a bit hard to understand, but you can think of this XPath code as an SQL statement, similar to the command to retrieve a recordset:
SELECT * from languages.language WHERE xml:lang= ' en '
To return to the actual XPath query, use the following code to return the node object that contains the first matching node:
Set SelectedNode = _
Doc.selectsinglenode (SEL)
The final step is to pass the element name ("title") to the selectSingleNode method and ask it to retrieve the Text property value of the "title" node. That is to say, take
Must be included in the <title> and </title> of the text. Here, the text retrieved is "Localize ASP with XML":
Txt=selectednode._
selectSingleNode ("title"). Text
In contrast to the SQL command, like retrieving a field value from within an ADO recordset, the statement is:
Txt=rs ("title")
Insert a language string in an ASP page
Knowing how to select text from an XML element and assign it to a variable, you can insert the value of the variable into the HTML stream. To make the code concise, create a name called
The GetString () function, as follows:
function GetString (instring)
Temp=selectednode._
selectSingleNode _
(instring). Text
getstring= _
Server.HTMLEncode (temp)
End Function
The input value of the GetString () function is the element name, and the element value is retrieved from the XML document. For example, to pass "FirstName" to GetString (),
The GetString function selects the FirstName element and returns its text value. For insurance purposes, before returning to the caller, we use the ASP's
The Server.HTMLEncode method converts the text to a valid HTML code. In the ASP page, the calling code resembles the following:
<td>
<%=getstring ("FirstName")%>:
</td>
If you select the Chinese part of the XML document, the HTML output is as follows:
<td>first name:v/td>
If the french,html output is selected, the results are as follows:
<td>Prénom:</td>
The following figure shows the French language version of the form:

Select available languages
One of the great advantages of XML is that it is saved in a clear text format, and we can update the XML files on the Web server at any time. Besides, we can also
Open the Chinese version of the Xmltrans.xml file in the XML Editor XML Nothpad, copy it, and then translate the text into a new language. Figure below
Shows the routine file in XML Notepad:

In the illustration above, you can add, delete, and select elements and attributes on the left, and you can edit the relevant content on the right. When a site needs multiple language versions
At this time, you just need to perform a paste operation in the content, and then upload the latest XML document.
For the added language to take effect immediately, add a program that determines the number of different languages in the file and returns the language code and language name.
As shown in the following code snippet, we can format the data to create an HTML list box. 2-Letter language type code when the user submits the form
is stored in the ASP session variable Choselang.
<select name= "Chosenlang" >
<%
For I=0 to Selectednodes.length-1
Response.Write "<option value=" & _
Chr & _
Selectednodes.item (i). _
selectSingleNode ("Lang"). Text & Chr (34)
if (Selectednodes.item (i). _
selectSingleNode ("Lang"). Text = _
Session ("Chosenlang") Then
Response.Write "Selected>"
Else
Response.Write ">"
End If
Response.Write Selectednodes.item (i). _
selectSingleNode ("Langname"). Text & _
"</option>" & vbCrLf
Next
%></select>
Add hint information and character set data
The above uses XML to provide the variable language text display of HTML forms, and then consider some more useful uses. For example, to select a language, add a
Message, simply embed the HTML tag <label&gt before and after the text, and provide the variable content as the Title property value from the XML. Similarly, to help browsers
Identify the language types of HTML pages and save character set information in an XML file, such as:
<charset>x-sjis</charset>
You can then insert the character set type into the HTML stream using asp:
<meta http-equiv= "Content-type" content= "text/html; charset=
<%=getstring ("charset")%> "/>
The final HTML code includes the Japanese character set reference:
<meta http-equiv= "Content-type" content= "text/html; Charset=x-sjis "/>
The following illustration shows a page that uses the Japanese character set. Remember, to see the correct content, the operating system and the browser must support Unicode and install a phase
should be character. Otherwise, you'll see some strange text, maybe some question marks, maybe some square symbols, and so on.

Summarize
This article describes how to save the language string for a Web page using an XML file. From this we learned to use ASP scripting and Microsoft XML Parser and the Consortium
XPath language implements the ability to query XML documents. The ASP code inserts the mutable text containing the character set type into the HTML stream and forms a list box to allow the user to select
Available languages. This article covers a lot of content, but I think it's just a starting point. Although these features can be implemented simply in browsers that support XML, we have
See: Using server-side scripts to convert XML data to plain HTML content, even in earlier versions of browsers, you can see these wonderful
Capacity.
Related Article

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.