xml PARSeR EXAMPLE 2:  GROUPS AND ATTRIBUTES

 

The following example shows how to specify that an XML element is a group for child elements and how to specify translatable XML attributes.  The use of the "rwt:group" and the "rwt:attributeTU_<attrName>" instruction attributes is also demonstrated.

 

Sample name:  "Sample2-GroupsAndAttributes"

Location of sample file:  ...\Samples\Developing\XMLParser\Sample2-GroupsAndAttributes

 

SAMPLE FILE

DESCRIPTION

1. "Sample.xml"

Input: An XML file to be translated and that is to be read by the XML parser.

2. "XMLParser-materials.xsl"

The XML parser  for "Samples.xml", an XSL extensible stylesheet.

3. "msxsl.exe"

Command line utility to run the XSL transformation.

4. "Transform.bat"

Batch file to run the XSL transformation.

5. "Transformed.xml"

Output: The transformed XML file containing RC-WinTrans instruction attributes.  Run the "Transform.bat" batch file to generate the file.

 

Related topics...

 

 

XML Parser

File name: "XMLParser-materials.xsl" (see also: "XML Parser File Naming Convention")

Parsed elements that are to be specified as translatable:

·

<material 'name'

·

<EX> 'displayname'

·

<DENS> 'displayname'

Parsed elements that are to be specified as a group: <materials> and <material>.

 

TEXT COLORS:

Black: Code of the template file for the XML parser.

Blue: Code specific to the XML file being parsed.

Green: XSL command used to add an RC-WinTrans instruction attribute.

Rose/Pink: The name of the RC-WinTrans instruction attribute.

^ Top ^

<?xml version="1.0" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

                xmlns:fn="http://www.w3.org/2005/02/xpath-functions"

                version="1.0">

   <xsl:output encoding="utf-8"/>

 

   <!-- match the root node -->

   <xsl: template  match="/">

      <xsl:text>

   </xsl:text>

    

   <xsl:apply-templates select="*|@*|text()|comment()|processing-instruction()"/>

   </xsl:template>

   <!-- match everything else -->

   <xsl:template match="*|@*|text()|comment()|processing-instruction()">

      <xsl:copy><xsl:apply-templates

          select="*|@*|text()|comment()|processing-instruction()"/></xsl:copy>

   </xsl:template>

 

   <!-- the element 'materials' as group -->

  <xsl:template match="materials">

   <xsl:element name="{local-name(.)}">

      <xsl:attribute name="rwt:group"
namespace = "RC-WinTrans">Materials </xsl:attribute>

      <xsl:apply-templates

             select="*|@*|text()|comment()|processing-instruction()"/>

   </xsl:element>

  </xsl:template>

 

  <!-- the element 'material - make a group and translate the 'name' attribute -->

  <xsl:template match="material">

   <xsl:element name="{local-name(.)}">

      <xsl:attribute name="rwt:group" namespace = "RC-WinTrans">
Material matid=<xsl:value-of select="@matid"/></xsl:attribute>

      <xsl:attribute name="rwt:attributeTU_name"
namespace = "RC-WinTrans"></xsl:attribute>

      <xsl:apply-templates

                select="*|@*|text()|comment()|processing-instruction()"/>

   </xsl:element>

  </xsl:template>

 

  <!-- 'EX' element - set the 'displayname' and 'descr' attributes as translatable -->

  <xsl:template match="EX">

   <xsl:element name="{local-name(.)}">

      <xsl:attribute name="rwt:attributeTU_displayname"
namespace = "RC-WinTrans"></xsl:attribute>

      <xsl:attribute name="rwt:attributeTU_desc"
namespace = "RC-WinTrans"></xsl:attribute>

      <xsl:apply-templates

                select="*|@*|text()|comment()|processing-instruction()"/>

   </xsl:element>

  </xsl:template>

 

  <!-- 'DENS' element - set the 'displayname' attribute as translatable -->

  <xsl:template match="DENS">

   <xsl:element name="{local-name(.)}">

      <xsl:attribute name="rwt:attributeTU_displayname"
namespace = "RC-WinTrans"> </xsl:attribute>

      <xsl:apply-templates

                select="*|@*|text()|comment()|processing-instruction()"/>

   </xsl:element>

  </xsl:template>

 

</xsl:stylesheet>

 

^ Top ^

 

XML File (To Be Translated)

File name: "Sample.xml"

Element attributes to be translated:

·

<material 'name'

·

<EX> 'displayname' and 'dec'

·

<DENS> 'displayname'

(Green = name of the XML attribute to be translated.)

 

Parsed elements that are to be specified as a group: <materials> and <material>.

 

<?xml version="1.0"?>

<materials>

  <material name="Alloy Steel" matid="3">

     <physicalproperties>

         <EX displayname="Elastic Modulus" value="0.21E+12" desc="none"/>

         <DENS displayname="Density" value="0.77E+04"/>

      </physicalproperties>

  </material>

  <material name="Cast Stainless Steel" matid="6">

     <physicalproperties>

        <EX displayname="Thermal Expansion Coefficient" value="10.9E+12" descr="none"/>

        <DENS displayname="Density" value=""/>

       </physicalproperties>

      </material>

</materials>

 

^ Top ^

 

Figure: The RC-WinTrans application where the XML file "Sample.xml" has been added to a translation project. The Project Data view (left) shows the group's hierarchy as specified in the XML parser.  The Text Table view (right) lists the texts of the XML attributes that are to be translated.

 

^ Top ^