We created some sample Xml data about three people

<?xml version="1.0" encoding="utf-8"?>
<people>
  <person name='Anna'>
    <age>23</age>
    <gender>female</gender>
  </person>
  <person name='Betty'>
    <age>17</age>
    <gender>female</gender>
  </person>
  <person name='Clark'>
    <gender>male</gender>
  </person>
</people>

We described our data for external applications in an Xsd file

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="people">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="person" >
          <xs:complexType>
            <xs:all>
              <xs:element name="age" minOccurs="0">
                <xs:simpleType>
                  <xs:restriction base="xs:integer">
                    <xs:minInclusive value="0"/>
                    <xs:maxInclusive value="120"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="gender">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:enumeration value="male"/>
                    <xs:enumeration value="female"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:all>
            <xs:attribute name="name" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

We created an Xsl Transformation to turn our Xml into an Html list

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="yes" />
  <!--  //    This match-expression '/' will match the root node of the input xml, 
        //    whatever it may be named -->
  <xsl:template match="/">
    <!--  //    We'll list the persons using an Html definition list -->
    <dl>
      <!--    //    For each person in people, sorted by age-->
      <xsl:for-each select="/people/person">
        <xsl:sort select="age"/>
        <!--  //    Apply template to the person -->
        <xsl:apply-templates select="." />
      </xsl:for-each>
    </dl>
  </xsl:template>
  <!--  //    Matches person elements from the sample people.xml -->
  <xsl:template match="person">
    <dt style="color: Green;">
      <xsl:value-of select="@name"/>
    </dt>
    <dd>
      <xsl:text> (</xsl:text>
      <xsl:value-of select="gender"/>
      <xsl:text>) </xsl:text>
    </dd>
    <xsl:choose>
      <!--  //    If an age exists for this person, include it-->
      <xsl:when test="age">
        <dd style="color: Blue;">
          <xsl:value-of select="age"/>
          <xsl:text> years</xsl:text>
        </dd>
      </xsl:when>
      <xsl:otherwise>
        <dd style="color: Red;">
          <xsl:text> Age Unknown</xsl:text>
        </dd>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

The transformation was applied to our Xml with this result Html

<dl>
  <dt style="color: Green;">Clark</dt>
  <dd> (male) </dd>
  <dd style="color: Red;"> Age Unknown</dd>
  <dt style="color: Green;">Betty</dt>
  <dd> (female) </dd>
  <dd style="color: Blue;">17 years</dd>
  <dt style="color: Green;">Anna</dt>
  <dd> (female) </dd>
  <dd style="color: Blue;">23 years</dd>
</dl>

The result Html can be displayed in a web-page like this

Clark
(male)
Age Unknown
Betty
(female)
17 years
Anna
(female)
23 years
Check XHTML « spb.xanderlih.com Copyright © Xander Lih 2000-2012  » Check CSS