Microsoft Access Database Development With VBA

The Extensible Style Sheet Transformations (XSLT)

 

Fundamentals of XSLT

 

Introduction

Like a cascading style sheet, an extensible style sheet directs the parser as to how to present an XML document to a user. The extensible style sheet uses a series of instructions destined to "transform" the contents of an XML document into friendlier formats.

As a result, the language is called Extensible Style Sheet Transformation or XSLT. XSLT is defined by the World Wide Web Consortium (W3C) at http://www.w3.org/TR/xslt.html. Besides following the XSLT standards, Microsoft defined its own interpretation of XSLT. In this lesson, we will follow XSLT as defined by Microsoft.

An extensible style sheet is a file with the .xsl extension. As seen for XML, if you double-click an XSLT file in a Windows Explorer or another file utility or manager, it would be opened in a Web browser (or in the default browser if you have more than one Web browser).

Before creating a style sheet, you may want to create, or at least start, your XML document first. This allows you to create or start creating the elements (and attributes) you will need. Also, this allows you to define or name your elements. Consider an XML document saved as RealEstate1.xml and the document's content is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<RealEstate>
  <ListingTitle>Real Estate - Properties Listing</ListingTitle>
  <RegionTitle>Washington, DC Metro Listing</RegionTitle>
</RealEstate>

To present an XML document in a friendly way to a user, you have various options. The primary solution is to create a regular XSLT document. Like an XML document, an XSLT document starts with a declaration as <?xml version="1.0" encoding="UTF-8"?>. As seen in XML, each element in XSLT must have a name. In XSLT, the name must have been defined in a namespace. The names used in XSLT are defined in a namespace specified in the http://www.w3.org/1999/XSL/Transform document. Therefore, you must reference that namespace in your XSLT document. Like XML, XSLT is defined in versions. The current version is 1.0.

The namespace that defines the names in XSLT documents is called xsl. Therefore, to access one of its names, you must qualify it by preceding it (the name in the namespace) with xsl and the qualifying operator, which is the colon ":".

The Document Element

As seen in XML, and as we will see in the next sections, an XSLT document is made of elements. And as mentioned for XML, all elements in an XSLT document must have a common ancester, called the root element, also called the document element. While XML lets you create your own root and give it any name you want, XSLT defines the root element as stylesheet. The syntax of the stylesheet element is:

<xsl:stylesheet
  id = id
  extension-element-prefixes = NCNames
  exclude-result-prefixes = NCNames
  version = number>
</xsl:stylesheet>

The id attribute is optional. It is mostly used if you want to embed one XSLT section (or code) in another. In this case, if you decide to use an id attribute, give it a value that is unique among the other ids. You must define the name of the extension-element-prefixes attribute. The attribute is used to specify the namespace that holds the names of the elements that will be used in the XSLT document. As a result:

  • Because the names of elements used in an XSLT document are defined in http://www.w3.org/1999/XSL/Transform, the value of the extension-element-prefixes attribute is that namespace
  • Because http://www.w3.org/1999/XSL/Transform is a namespace, you must reference it as such (a namespace), which is done using the xmlns prefix. Now, because the namespace name defined is xsl, you can use it as the name of the attribute

As a result, an XSLT document starts as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"></xsl:stylesheet>

By the way, the version attribute can come before or after the namespace:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"></xsl:stylesheet>

Make sure you save the document with the .xsl extension (for our example, we save it as RealEstate.xsl).

As another, in fact a better, way to present an XML document to a user, you can include (or embed) HTML code in your extensible style sheet document, between the start and end tags of the stylesheet element. This can start as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <html>
    <head>
      <title>Real Estate - Properties Listing</title>
    </head>
    <body>

    </body>
  </html>
</xsl:stylesheet>

Qualified Names

In XML, each element must have a name. In XSLT too, each element also must have a name but the name must have been defined in a namespace. As mentioned already, the names used in XSLT are defined in the http://www.w3.org/1999/XSL/Transform namespace. After referencing the XSLT namespace, to use a name, you must qualify it by preceding it with xsl>.

Referencing an XSLT Document

In your XML document, you must reference the style sheet, which is done after the XML declaration. The basic formula to reference the extensible style sheet is:

<?xml-stylesheet href="Path or FileName.xsl" type="text/xsl"?>

Replace FileName with the name of the style sheet. Here is an example:

Private Sub cmdCreateXMLFile_Click()
On Error GoTo cmdCreateXMLFile_Error

    Open "RealEstate1.xml" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<?xml-stylesheet href=""RealEstate1.xsl"" type=""text/xsl"" ?>"
    Print #1, "<RealEstate>"
    Print #1, "  <ListingTitle>Real Estate - Properties Listing</ListingTitle>"
    Print #1, "  <RegionTitle>Washington, DC Metropolitan Area</RegionTitle>"
    Print #1, "</RealEstate>"

    Close #1
    
    Exit Sub

cmdCreateXMLFile_Error:
    MsgBox "There was a problem when trying to create the XML file.", _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

This would produce:

Transforming an XML Element

XSL Templates

Besides specifying the names of elements, the XSLT standard also defines how the elements of an XML document are accessed. This definition is carried by an element named template. The syntax of the template element is:

<xsl:template
  name= Qname
  match = Pattern
  priority = number
  mode = QName
</xsl:template>

The name attribute holds a common name, a namespace name in which case it would be preceded by a prefix, or a Web address. If you pass

  • A regular name, the parser would try to find it in the XML document. If that name is not found, the parser would ignore it (no error would be produced). If the parser finds that name, it would use it
  • If you pass a name from a namespace, you must precede it with a prefix. In this case, the parser would look for that name in the namespace specified in the declaration. If the parser finds that name, it would use it for whatever you are trying to do. If that name is not found in the namespace, the parser would ignore it and would not produce an error

The match attribute specifies how, or from where, the parser should start building a complete name (like a file path in Windows Explorer) for an element. For example, if you specify its value as / or as the name of the root, the parser would start from the root element. You can also specify the name of another element, depending on what you are trying to do. The template element must have either a name or a match attribute or both. Here is an example:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match=...>
    <html>
      <head>
      <title>Real Estate - Peoperties Listing</title>
      </head>
      <body>
   
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Raw Text

The extensible style sheet language allows you to add basic text to its document. To do this, create an element named text and set its value as the text you want. Here is an example:

Private Sub cmdTransform_Click()
On Error GoTo cmdTransform_Error

    Open "C:\RealEstate\RealEstate0.xsl" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"">"
    Print #1, "  <xsl:template match=""/"">"
    Print #1, "    <html>"
    Print #1, "    <head>"
    Print #1, "    <title>Real Estate - Peoperties Listing</title>"
    Print #1, "    </head>"
    Print #1, "    <body>"
    Print #1, "      <div align=""center"">"
    Print #1, "        <p><xsl:text>This is a list of the properties that our real estate company is currently offering.</xsl:text></p>"
    Print #1, "      </div>"
    Print #1, "    </body>"
    Print #1, "    </html>"
    Print #1, "  </xsl:template>"
    Print #1, "</xsl:stylesheet>"

    Close #1
    
    Exit Sub

cmdTransform_Error:
    MsgBox "There was a problem to create the XSLT file. Please report the problem as:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

This would produce:

Transforming an XML Element

Transforming an XML Element

 

Introduction

The purpose of an extensible style sheet is to better display the values of an XML document. The primary object that holds a value in an XML document is an XML element. This means that you must "transform" an element to display its value. In the XSLT document, specify what value to display and when. To do this, in the body section, create an element named value-of. The syntax of the value-of element is:

<xsl:value-of
  select = Expression
  disable-output-escaping = "yes" | "no" 
</xsl:value-of>

The element must have an attribute named select that holds an expression that the parser must follow to identify an element. If you specify the match value of the template element as the name of the root, the value of the select attribute of the value-of element can be the name of an element that is a child of the root. Here is an example:

Private Sub cmdTransform_Click()
On Error GoTo cmdTransform_Error

    Open "C:\RealEstate\RealEstate1.xsl" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"">"
    Print #1, "  <xsl:template match=""RealEstate"">"
    Print #1, "    <html>"
    Print #1, "    <head>"
    Print #1, "    <title>Real Estate - Peoperties Listing</title>"
    Print #1, "    </head>"
    Print #1, "    <body>"
    Print #1, "      <div align=""center"">"
    Print #1, "        <h2><xsl:value-of select=""ListingTitle"" /></h2>"
    Print #1, "      </div>"
    Print #1, "    </body>"
    Print #1, "    </html>"
    Print #1, "  </xsl:template>"
    Print #1, "</xsl:stylesheet>"

    Close #1
    
    Exit Sub

cmdTransform_Error:
    MsgBox "There was a problem to create the XSLT file. Please report the problem as:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

This would produce:

Transforming an XML Element

If you specify the match value of the template element as "/", if you want to access an element that is a child of the root, precede its name with the name of the root for the value of the select attribute of the value-of element. The above document is the same as:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <head>
      <title>Real Estate - Properties Listing</title>
      </head>
      <body>
        <div align="center">

          <h2><xsl:value-of select="RealEstate/ListingTitle" /></h2>
  
        </div>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

This would produce the same result as previously. In the same way, you can create the styles for other elements of the XML document. Here are examples:

Private Sub cmdTransform_Click()
On Error GoTo cmdTransform_Error

    Open "C:\RealEstate\RealEstate1.xsl" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"">"
    Print #1, "  <xsl:template match=""/"">"
    Print #1, "    <html>"
    Print #1, "    <head>"
    Print #1, "    <title>Real Estate - Peoperties Listing</title>"
    Print #1, "    </head>"
    Print #1, "    <body>"
    Print #1, "      <div align=""center"">"
    Print #1, "        <h2><xsl:value-of select=""RealEstate/ListingTitle"" /></h2>"
    Print #1, "        <h3><xsl:value-of select=""RealEstate/RegionTitle"" /></h3>"
    Print #1, "      </div>"
    Print #1, "    </body>"
    Print #1, "    </html>"
    Print #1, "  </xsl:template>"
    Print #1, "</xsl:stylesheet>"

    Close #1
    
    Exit Sub

cmdTransform_Error:
    MsgBox "There was a problem to create the XSLT file. Please report the problem as:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

This would produce:

Transforming an XML Element

Applying Templates

Another technique consists of creating one or a series of templates and applying them in the desired section(s). In this case, create a template for each element. In the area where you want to use the format(s), create an XSLT element named apply-templates. The syntax of the apply-templates element is:

<xsl:apply-templates
  select = Expression
  mode = QName
</xsl:apply-templates>

Both attributes are optional. Here is an example:

Private Sub cmdTransform_Click()
On Error GoTo cmdTransform_Error

    Open "C:\RealEstate\RealEstate1.xsl" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"">"
    Print #1, "  <xsl:template match=""/RealEstate"">"
    Print #1, "    <html>"
    Print #1, "    <head>"
    Print #1, "    <title>Real Estate - Peoperties Listing</title>"
    Print #1, "    </head>"
    Print #1, "    <body>"
    Print #1, "      <div align=""center"">"
    Print #1, "        <xsl:apply-templates />"
    Print #1, "      </div>"
    Print #1, "    </body>"
    Print #1, "    </html>"
    Print #1, "  </xsl:template>"
    Print #1, "  <xsl:template match=""ListingTitle"">"
    Print #1, "    <h1><xsl:value-of select=""."" /></h1>"
    Print #1, "  </xsl:template>"
    Print #1, "  <xsl:template match=""RegionTitle"">"
    Print #1, "    <h4><xsl:value-of select=""."" /></h4>"
    Print #1, "  </xsl:template>"
    Print #1, "</xsl:stylesheet>"

    Close #1
    
    Exit Sub

cmdTransform_Error:
    MsgBox "There was a problem to create the XSLT file. Please report the problem as:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
           vbOKOnly Or vbInformation, _
           "Extensible Markup Language"
    Resume Next
End Sub

This would produce:

Transforming an XML Element

Transforming an XML Element

If you want to provide the nodes that the parser must consider, pass them in the select attribute. The mode is used by the parser to process an element multiple times. In this case, pass the name of the element to this attribute.

 
 
 

Transforming a Set of XML Elements

Instead of transforming one element at a time, you may have a series of elements and it would not be flexible to visit each element. In fact. it may be impossible to visit each element if the list of elements can grow or shrink any time. If the elements are child nodes, you can apply a loop to their parent and visit each child node. To support this operation, XSLT provides an element named for-each. The syntax to use the for-each element is:

<xsl:for-each
  select = Expression
</xsl:for-each>

As done for the elements so far, the select attribute allows you to specify the element to use. Consider the following XML document saved as RealEstate2.xml:

Private Sub cmdCreateXMLFile_Click()
On Error GoTo cmdCreateXMLFile_Error

    Open "C:\RealEstate\RealEstate2.xml" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<?xml-stylesheet href=""C:\RealEstate\RealEstate2.xsl"" type=""text/xsl"" ?>"
    Print #1, "<RealEstate>"
    Print #1, "  <ListingTitle>Real Estate - Properties Listing</ListingTitle>"
    Print #1, "  <Property>"
    Print #1, "    <PropertyNumber>808420</PropertyNumber>"
    Print #1, "    <PropertyType>Single Family</PropertyType>"
    Print #1, "    <Bedrooms>4</Bedrooms>"
    Print #1, "    <Bathrooms>2.50</Bathrooms>"
    Print #1, "    <City>Silver Spring</City>"
    Print #1, "    <State>MD</State>"
    Print #1, "  </Property>"
    Print #1, "  <Property>"
    Print #1, "    <PropertyNumber>209538</PropertyNumber>"
    Print #1, "    <PropertyType>Townhouse</PropertyType>"
    Print #1, "    <Bedrooms>3</Bedrooms>"
    Print #1, "    <Bathrooms>2.50</Bathrooms>"
    Print #1, "    <City>Washington</City>"
    Print #1, "    <State>DC</State>"
    Print #1, "    <MarketValue>258605</MarketValue>"
    Print #1, "  </Property>"
    Print #1, "  <Property>"
    Print #1, "    <PropertyNumber>740514</PropertyNumber>"
    Print #1, "    <PropertyType>Condominium</PropertyType>"
    Print #1, "    <Bedrooms>1</Bedrooms>"
    Print #1, "    <Bathrooms>1.00</Bathrooms>"
    Print #1, "    <City>McLean</City>"
    Print #1, "  </Property>"
    Print #1, "  <Property>"
    Print #1, "    <PropertyNumber>407384</PropertyNumber>"
    Print #1, "    <PropertyType>Single Family</PropertyType>"
    Print #1, "    <Bedrooms>5</Bedrooms>"
    Print #1, "    <Bathrooms>3.50</Bathrooms>"
    Print #1, "    <City>Silver Spring</City>"
    Print #1, "    <MarketValue>622015</MarketValue>"
    Print #1, "  </Property>"
    Print #1, "</RealEstate>"

    Close #1
    
    Exit Sub

cmdCreateXMLFile_Error:
    MsgBox "There was a problem when trying to create the XML file." & vbCrLf & _
           "Make sure you report the error as follows:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

This is a typical list to which new elements (new houses in this case) can be added any time and undesired elements (such as sold houses) can be deleted any time. So it is not practical to visit each node. In fact, elements on the same level may not have the same child nodes and child nodes may not be arranged in the same order in each element. You can first create an XSLT with a placeholder for each element. Here is an example:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
    <head>
    <title>Real Estate - Properties Listing</title>
    </head>
    <body>
    <h3>Real Estate - Peoperties Lising</h3>
 
    <table>
      <tr>
        <td><b>Prop #</b></td>
        <td><b>Type</b></td>
        <td><b>Bedrooms</b></td>
        <td><b>Bathrooms</b></td>
        <td><b>City</b></td>
        <td><b>State</b></td>
        <td><b>Market Value</b></td>
      </tr>
      <tr>
        <td>Placeholder for property #</td>
        <td>Placeholder for property type</td>
        <td>Placeholder for number of bedrooms</td>
        <td>Placeholder for number of bathrooms</td>
        <td>Placeholder for the city</td>
        <td>Placeholder for state</td>
        <td>Placeholder for house house price (value in the market)</td>
      </tr>
    </table>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

A solution is to use a for-each element to visit each child node. This can be done as follows:

Private Sub cmdTransform_Click()
On Error GoTo cmdTransform_Error

    Open "C:\RealEstate\RealEstate2.xsl" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"">"
    Print #1, "  <xsl:template match=""/"">"
    Print #1, "    <html>"
    Print #1, "    <head>"
    Print #1, "    <title>Real Estate - Peoperties Listing</title>"
    Print #1, "    </head>"
    Print #1, "    <body>"
    Print #1, "      <div align=""center"">"
    Print #1, "        <h3><xsl:value-of select=""RealEstate/ListingTitle"" /></h3>"
    Print #1, "        <table>"
    Print #1, "          <tr bgcolor=""#B8B8B8"">"
    Print #1, "            <td width=""60"" align=""center""><b>Prop #</b></td>"
    Print #1, "            <td width=""100""><b>Type</b></td>"
    Print #1, "            <td><b>Bedrooms</b></td>"
    Print #1, "            <td><b>Bathrooms</b></td>"
    Print #1, "            <td><b>City</b></td>"
    Print #1, "            <td><b>State</b></td>"
    Print #1, "            <td><b>Market Value</b></td>"
    Print #1, "          </tr>"
    Print #1, "          <xsl:for-each select=""RealEstate/Property"">"
    Print #1, "          <tr bgcolor=""#E2E2E2"">"
    Print #1, "            <td align=""center""><xsl:value-of select=""PropertyNumber"" /></td>"
    Print #1, "            <td><xsl:value-of select=""PropertyType"" /></td>"
    Print #1, "            <td align=""center""><xsl:value-of select=""Bedrooms"" /></td>"
    Print #1, "            <td align=""center""><xsl:value-of select=""Bathrooms"" /></td>"
    Print #1, "            <td><xsl:value-of select=""City"" /></td>"
    Print #1, "            <td align=""center""><xsl:value-of select=""State"" /></td>"
    Print #1, "            <td align=""center""><xsl:value-of select=""MarketValue"" /></td>"
    Print #1, "          </tr>"
    Print #1, "          </xsl:for-each>"
    Print #1, "        </table>"""
    Print #1, "      </div>"
    Print #1, "    </body>"
    Print #1, "    </html>"
    Print #1, "  </xsl:template>"
    Print #1, "</xsl:stylesheet>"

    Close #1
    
    Exit Sub

cmdTransform_Error:
    MsgBox "There was a problem to create the XSLT file. Please report the problem as:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

This would produce:

Transforming XML Attributes

As you may know already, an XML atribute is a child element created inside of the start tag of a node. As a reminder, here is an example of an attribute:

Private Sub cmdCreateXMLFile_Click()
On Error GoTo cmdCreateXMLFile_Error

    Open "C:\RealEstate\RealEstate3.xml" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<?xml-stylesheet href=""C:\RealEstate\RealEstate2.xsl"" type=""text/xsl"" ?>"
    Print #1, "<RealEstate>"
    Print #1, "  <ListingTitle Category=""Regional Listing"">Real Estate - Properties Listing</ListingTitle>"
    Print #1, "</RealEstate>"

    Close #1
    
    Exit Sub

cmdCreateXMLFile_Error:
    MsgBox "There was a problem when trying to create the XML file." & vbCrLf & _
           "Make sure you report the error as follows:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

Because an attribute is related to the element, to access an attribute in an extensible style sheet, you can precede its name with the @ sign, but you must indicate its parent element. This can be done in the template element where you would provide the name of its parent element. Here is an example:

Private Sub cmdTransform_Click()
On Error GoTo cmdTransform_Error

    Open "C:\RealEstate\RealEstate3.xsl" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"">"
    Print #1, "  <xsl:template match=""ListingTitle"">"
    Print #1, "    <html>"
    Print #1, "    <head>"
    Print #1, "    <title>Real Estate - Peoperties Listing</title>"
    Print #1, "    </head>"
    Print #1, "    <body>"
    Print #1, "      <div align=""center"">"
    Print #1, "        <xsl:value-of select=""@Category"" />"
    Print #1, "      </div>"
    Print #1, "    </body>"
    Print #1, "    </html>"
    Print #1, "  </xsl:template>"
    Print #1, "</xsl:stylesheet>"

    Close #1
    
    Exit Sub

cmdTransform_Error:
    MsgBox "There was a problem to create the XSLT file. Please report the problem as:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

In the same way, you can transform other XML attributes from your style sheet. Also you can format the values of the attributes any way you want. Here are examples:

XML File: RealEstate4.xml

Private Sub cmdCreateXMLFile_Click()
On Error GoTo cmdCreateXMLFile_Error

    Open "C:\RealEstate\RealEstate4.xml" For Output As #1

    Print #1, "<?xml version='1.0' encoding='UTF-8'?>"
    Print #1, "<?xml-stylesheet href='C:\RealEstate\RealEstate4.xsl' type='text/xsl' ?>"
    Print #1, "<RealEstate>"
    Print #1, "  <RegionTitle BuiltFrom='1970'"
    Print #1, "               BuiltTo = '2020'"
    Print #1, "               PriceFrom = '200000'"
    Print #1, "               PriceTo='800000'>Washington, DC Metropolitan Area</RegionTitle>"
    Print #1, "</RealEstate>"

    Close #1
    
    Exit Sub

cmdCreateXMLFile_Error:
    MsgBox "There was a problem when trying to create the XML file." & vbCrLf & _
           "Make sure you report the error as follows:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

XSLT File: RealEstate4.xsl

Private Sub cmdTransform_Click()
On Error GoTo cmdTransform_Error

    Open "C:\RealEstate\RealEstate4.xsl" For Output As #1

    Print #1, "<?xml version='1.0' encoding='UTF-8'?>"
    Print #1, "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>"
    Print #1, "  <xsl:template match='RegionTitle'>"
    Print #1, "    <html>"
    Print #1, "    <head>"
    Print #1, "    <title>Real Estate - Properties Listing</title>"
    Print #1, "    </head>"
    Print #1, "    <body>"
    Print #1, "      <p><strong>Year Range From </strong><xsl:value-of select='@BuiltFrom' /><strong> to </strong><xsl:value-of select='@BuiltTo' /></p>"
    Print #1, "      <p><strong>Price Range From</strong> $<xsl:value-of select='@PriceFrom' /><strong> to </strong>$<xsl:value-of select='@PriceTo' /></p>"
    Print #1, "    </body>"
    Print #1, "    </html>"
    Print #1, "  </xsl:template>"
    Print #1, "</xsl:stylesheet>"

    Close #1
    
    Exit Sub

cmdTransform_Error:
    MsgBox "There was a problem to create the XSLT file. Please report the problem as:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

In the above examples, we considered only one element with attributes. Most of the time, you may have different elements that have attributes. In such a case, you can specify the value of the match attribute of the template as the root or an ancestor. Consider the following version of the XML document (saved as RealEstate4.xml):

Private Sub cmdCreateXMLFile_Click()
On Error GoTo cmdCreateXMLFile_Error

    Open "C:\RealEstate\RealEstate4.xml" For Output As #1

    Print #1, "<?xml version='1.0' encoding='UTF-8'?>"
    Print #1, "<?xml-stylesheet href='C:\RealEstate\RealEstate4.xsl' type='text/xsl' ?>"
    Print #1, "<RealEstate>"
    Print #1, "  <ListingTitle Category='Regional Listing'>Real Estate - Properties Listing</ListingTitle>"
    Print #1, "    <RegionTitle BuiltFrom='1970'"
    Print #1, "               BuiltTo = '2020'"
    Print #1, "               PriceFrom = '200000'"
    Print #1, "               PriceTo='800000'>Washington, DC Metropolitan Area</RegionTitle>"
    Print #1, "</RealEstate>"

    Close #1
    
    Exit Sub

cmdCreateXMLFile_Error:
    MsgBox "There was a problem when trying to create the XML file." & vbCrLf & _
           "Make sure you report the error as follows:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

To access an attribute, use the following formula:

ParentElement/@AttributeName

Here are example (this is a new version of the previous XSLT file: RealEstate4.xsl:

Private Sub cmdTransform_Click()
On Error GoTo cmdTransform_Error

    Open "C:\RealEstate\RealEstate4.xsl" For Output As #1

    Print #1, "<?xml version='1.0' encoding='UTF-8'?>"
    Print #1, "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>"
    Print #1, "  <xsl:template match='RealEstate'>"
    Print #1, "    <html>"
    Print #1, "    <head>"
    Print #1, "    <title>Real Estate - Properties Listing</title>"
    Print #1, "    </head>"
    Print #1, "    <body>"
    Print #1, "      <xsl:value-of select='ListingTitle/@Category' />"
    Print #1, "      <table>"
    Print #1, "        <tr>"
    Print #1, "            <td><strong>Year Range From:</strong></td>"
    Print #1, "            <td><xsl:value-of select='RegionTitle/@BuiltFrom' /></td>"
    Print #1, "          </tr>"
    Print #1, "          <tr>"
    Print #1, "            <td align='right'><strong>to:</strong></td>"
    Print #1, "            <td><xsl:value-of select='RegionTitle/@BuiltTo' /></td>"
    Print #1, "          </tr>"
    Print #1, "          <tr>"
    Print #1, "            <td><strong>Price Range From:</strong></td>"
    Print #1, "            <td>$<xsl:value-of select='RegionTitle/@PriceFrom' /></td>"
    Print #1, "          </tr>"
    Print #1, "          <tr>"
    Print #1, "            <td align='right'><strong>to:</strong></td>"
    Print #1, "            <td>$<xsl:value-of select='RegionTitle/@PriceTo' /></td>"
    Print #1, "          </tr>"
    Print #1, "        </table>"
    Print #1, "    </body>"
    Print #1, "    </html>"
    Print #1, "  </xsl:template>"
    Print #1, "</xsl:stylesheet>"

    Close #1
    
    Exit Sub

cmdTransform_Error:
    MsgBox "There was a problem to create the XSLT file. Please report the problem as:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

This would produce:

In the same, you can visit the attributes in an element and access each of them using the @ sign. Consider the following document saved as RealEstate5.xml:

Private Sub cmdCreateXMLFile_Click()
On Error GoTo cmdCreateXMLFile_Error

    Open "C:\RealEstate\RealEstate5.xml" For Output As #1

    Print #1, "<?xml version='1.0' encoding='UTF-8'?>"
    Print #1, "<?xml-stylesheet href='C:\RealEstate\RealEstate5.xsl' type='text/xsl' ?>"
    Print #1, "<RealEstate>"
    Print #1, "  <ListingTitle>Real Estate - Peoperties Listing</ListingTitle>"
    Print #1, "  <Property>"
    Print #1, "    <PropertyNumber>808420</PropertyNumber>"
    Print #1, "      <PropertyType>Single Family</PropertyType>"
    Print #1, "      <Bedrooms>4</Bedrooms>"
    Print #1, "      <Bathrooms>2.50</Bathrooms>"
    Print #1, "      <City Locality=""White Oak"" Street=""New Hampshire Ave"">Silver Spring</City>"
    Print #1, "    <State ZIPCode=""20904"">MD</State>"
    Print #1, "  </Property>"
    Print #1, "  <Property>"
    Print #1, "    <PropertyNumber>209538</PropertyNumber>"
    Print #1, "    <PropertyType>Townhouse</PropertyType>"
    Print #1, "    <Bedrooms>3</Bedrooms>"
    Print #1, "    <Bathrooms>2.50</Bathrooms>"
    Print #1, "    <City Locality=""Georgetown"" Street=""M Str. N.W."">Washington</City>"
    Print #1, "    <State>DC</State>"
    Print #1, "    <MarketValue>258605</MarketValue>"
    Print #1, "  </Property>"
    Print #1, "  <Property>"
    Print #1, "    <PropertyNumber>740514</PropertyNumber>"
    Print #1, "    <PropertyType>Condominium</PropertyType>"
    Print #1, "    <Bedrooms>1</Bedrooms>"
    Print #1, "    <Bathrooms>1.00</Bathrooms>"
    Print #1, "    <City Locality=""Seven-Corner"">McLean</City>"
    Print #1, "  </Property>"
    Print #1, "  <Property>"
    Print #1, "    <PropertyNumber>407384</PropertyNumber>"
    Print #1, "    <PropertyType>Single Family</PropertyType>"
    Print #1, "    <Bedrooms>5</Bedrooms>"
    Print #1, "    <Bathrooms>3.50</Bathrooms>"
    Print #1, "    <City Locality=""Takoma Park Metro Station"" Street=""East-West Hwy"">Silver Spring</City>"
    Print #1, "    <MarketValue>622015</MarketValue>"
    Print #1, "  </Property>"
    Print #1, "</RealEstate>"

    Close #1
    
    Exit Sub

cmdCreateXMLFile_Error:
    MsgBox "There was a problem when trying to create the XML file." & vbCrLf & _
           "Make sure you report the error as follows:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

Notice that some elements have no attribute and some elements have one or more attributes. There are various ways you can access the attribute of an element in your extensible style sheet. Here are examples:

Private Sub cmdTransform_Click()
On Error GoTo cmdTransform_Error

    Open "C:\RealEstate\RealEstate5.xsl" For Output As #1

    Print #1, "<?xml version=""1.0""?>"
    Print #1, "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">"
    Print #1, "  <xsl:template match=""/"">"
    Print #1, "    <html>"
    Print #1, "    <head>"
    Print #1, "    <title>Real Estate - Peoperties Listing</title>"
    Print #1, "    </head>"
    Print #1, "    <body>"
    Print #1, "      <div align=""center"">"
    Print #1, "        <h3><xsl:value-of select=""RealEstate/ListingTitle"" /></h3>"
    Print #1, "        <table>"
    Print #1, "          <tr bgcolor=""#B8B8B8"">"
    Print #1, "            <td width=""60"" align=""center""><b>Prop #</b></td>"
    Print #1, "            <td width=""100""><b>Type</b></td>"
    Print #1, "            <td><b>Bedrooms</b></td>"
    Print #1, "            <td><b>Bathrooms</b></td>"
    Print #1, "            <td><b>City</b></td>"
    Print #1, "            <td><b>Locality</b></td>"
    Print #1, "            <td><b>On/Near</b></td>"
    Print #1, "            <td><b>State</b></td>"
    Print #1, "            <td><b>Market Value</b></td>"
    Print #1, "          </tr>"
    Print #1, "          <xsl:for-each select=""RealEstate/Property"">"
    Print #1, "          <tr bgcolor=""#E2E2E2"">"
    Print #1, "            <td align=""center""><xsl:value-of select=""PropertyNumber"" /></td>"
    Print #1, "            <td><xsl:value-of select=""PropertyType"" /></td>"
    Print #1, "            <td align=""center""><xsl:value-of select=""Bedrooms"" /></td>"
    Print #1, "            <td align=""center""><xsl:value-of select=""Bathrooms"" /></td>"
    Print #1, "            <td><xsl:value-of select=""City"" /></td>"
    Print #1, "            <td><xsl:value-of select=""City/@Locality"" /></td>"
    Print #1, "            <td><xsl:value-of select=""City/@Street"" /></td>"
    Print #1, "            <td align=""center""><xsl:value-of select=""State"" /></td>"
    Print #1, "            <td align=""center""><xsl:value-of select=""MarketValue"" /></td>"
    Print #1, "          </tr>"
    Print #1, "          </xsl:for-each>"
    Print #1, "        </table>"
    Print #1, "      </div>"
    Print #1, "    </body>"
    Print #1, "    </html>"
    Print #1, "  </xsl:template>"
    Print #1, "</xsl:stylesheet>"

    Close #1
    
    Exit Sub

cmdTransform_Error:
    MsgBox "There was a problem to create the XSLT file. Please report the problem as:" & vbCrLf & _
           "Error #: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description, _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

This would produce:

In the XSLT standards, the formula to transform an attribute is:

<xsl:attribute
  name = "attribute-name"  
  namespace = "uri-reference">
</xsl:attribute>

The name attribute is required. It can be a qualified or a local name. The namespace attribute is the address that defines where the transformating attribute was defined.

 
 
   
 

Previous Copyright © 2013 FunctionX, Inc. Next