Sie sind auf Seite 1von 20

XML A ND X S LT

Sadah Anjum Shanto


Lecturer,
Dept. of CSE
XML NAMESPACES
• name conflicts
• in xml, element names are defined by the developer. this often results in a conflict when
trying to mix xml documents from different xml applications.
• THIS XML CARRIES HTML TABLE • THIS XML CARRIES INFORMATION ABOUT
INFORMATION: A TABLE (A PIECE OF FURNITURE):
<table> <table>
<tr> <name>african coffee table</name>
<td>apples</td> <width>80</width>
<td>bananas</td> <length>120</length>
</tr>
</table>
</table>
SOLVING THE NAME CONFLICT USING A PREFIX
<h:table>
<h:tr>
• name conflicts in xml can <h:td>Apples</h:td>
easily be avoided using a <h:td>Bananas</h:td>
name prefix. </h:tr>
</h:table>
this xml carries information
about an html table, and a <f:table>
piece of furniture: <f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
XML NAMESPACES - THE XMLNS ATTRIBUTE

• when using prefixes in xml, a namespace for the prefix must be defined.


• the namespace can be defined by an xmlns attribute in the start tag of an element.
• the namespace declaration has the following syntax. xmlns:prefix="uri".
XML NAMESPACES - THE XMLNS ATTRIBUTE
• <root> <f:table
• <h:table xmlns:f="https://www.w3schools.com/furniture">
xmlns:h="http://www.w3.org/tr/html4/">
<f:name>african coffee table</f:name>
• <h:tr> <f:width>80</f:width>
• <h:td>apples</h:td> <f:length>120</f:length>
• <h:td>bananas</h:td> </f:table>
• </h:tr> </root>
• </h:table>
NAMESPACES IN REAL USE

• xslt is a language that can be used to transform xml documents into other formats.
• the xml document below, is a document used to transform xml into html.
• the namespace "http://www.w3.org/1999/xsl/transform" identifies xslt elements inside an
html document:
<tr>
• <?xml version="1.0" encoding="utf-8"?>       <td><xsl:value-of select="title"/></td>
<xsl:stylesheet version="1.0" xmlns:xsl="http       <td><xsl:value-of select="artist"/></td>
://www.w3.org/1999/xsl/transform">     </tr>
    </xsl:for-each>
  </table>
<xsl:template match="/"> </body>
<html> </html>
<body> </xsl:template>
  <h2>my cd collection</h2>
  <table border="1"> </xsl:stylesheet>
    <tr>
      <th style="text-align:left">title</th>
      <th style="text-align:left">artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
XSLT <XSL:TEMPLATE> ELEMENT

• the <xsl:template> element is used to build templates.


• the match attribute is used to associate a template with an xml element. the match
attribute can also be used to define a template for the entire xml document. the value of
the match attribute is an xpath expression (i.e. match="/" defines the whole document).
XSLT <XSL:TEMPLATE> ELEMENT
• <?xml version="1.0" encoding="utf-8"?> • <tr>
<xsl:stylesheet version="1.0"       <td>.</td>
xmlns:xsl="http://www.w3.org/1999/xsl/transform">       <td>.</td>
•     </tr>
<xsl:template match="/">   </table>
  <html>   </body>
  <body>   </html>
  <h2>my cd collection</h2> </xsl:template>
  <table border="1">
    <tr bgcolor="#9acd32">
</xsl:stylesheet>
      <th>title</th>
      <th>artist</th>
    </tr>
XSLT <XSL:VALUE-OF> ELEMENT
• the <xsl:value-of> element can be used to extract the value of an xml element and add it
to the output stream of the transformation:
• <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/xsl/transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>my cd collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>title</th>
      <th>artist</th>
    </tr>
    <tr>
      <td><xsl:value-of select="catalog/cd/title"/></td>
      <td><xsl:value-of select="catalog/cd/artist"/></td>
    </tr>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
• EXAMPLE EXPLAINED
• NOTE: THE SELECT ATTRIBUTE, IN THE EXAMPLE ABOVE, CONTAINS AN XPATH
EXPRESSION. AN XPATH EXPRESSION WORKS LIKE NAVIGATING A FILE
SYSTEM; A FORWARD SLASH (/) SELECTS SUBDIRECTORIES.

• THE RESULT FROM THE EXAMPLE ABOVE WAS A LITTLE DISAPPOINTING; ONLY
ONE LINE OF DATA WAS COPIED FROM THE XML DOCUMENT TO THE OUTPUT. IN
THE NEXT CHAPTER YOU WILL LEARN HOW TO USE THE <XSL:FOR-EACH>
ELEMENT TO LOOP THROUGH THE XML ELEMENTS, AND DISPLAY ALL OF THE
RECORDS.
THE <XSL:FOR-EACH> ELEMENT
• THE XSL <XSL:FOR-EACH> ELEMENT CAN BE USED TO SELECT EVERY XML
ELEMENT OF A SPECIFIED NODE-SET:

• <xsl:for-each select="catalog/cd">
XSLT <XSL:SORT> ELEMENT
• TO SORT THE OUTPUT, SIMPLY ADD AN <XSL:SORT> ELEMENT INSIDE THE
<XSL:FOR-EACH> ELEMENT IN THE XSL FILE:

• <xsl:for-each select="catalog/cd">
      <xsl:sort select="artist"/>
XSLT <XSL:IF> ELEMENT
• THE <XSL:IF> ELEMENT
• TO PUT A CONDITIONAL IF TEST AGAINST THE CONTENT OF THE XML FILE, ADD AN
<XSL:IF> ELEMENT TO THE XSL DOCUMENT.

• SYNTAX
• <xsl:if test="expression">
  ...some output if the expression is true...
</xsl:if>
WHERE TO PUT THE <XSL:IF> ELEMENT
• <xsl:for-each select="catalog/cd">
      <xsl:if test="price &gt; 10">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
          <td><xsl:value-of select="price"/></td>
        </tr>
      </xsl:if>
    </xsl:for-each>
XSLT <XSL:CHOOSE> ELEMENT
• SYNTAX
• <xsl:choose>
  <xsl:when test="expression">
    ... some output ...
  </xsl:when>
  <xsl:otherwise>
    ... some output ....
  </xsl:otherwise>
</xsl:choose>
WHERE TO PUT THE CHOOSE CONDITION
<xsl:for-each select="catalog/cd">

<tr>

<td><xsl:value-of select="title"/></td>

<xsl:choose>

<xsl:when test="price &gt; 10">

<td bgcolor="#ff00ff">

<xsl:value-of select="artist"/></td>

</xsl:when>

<xsl:otherwise>

<td><xsl:value-of select="artist"/></td>

</xsl:otherwise>

</xsl:choose>

</tr>

</xsl:for-each>
EXAMPLE
• HTTPS://WWW.W3SCHOOLS.COM/XML/TRYXSLT.ASP?XMLFILE=CDCATALOG&XSLT
FILE=CDCATALOG_CHOOSE2

• OUTPUT

Das könnte Ihnen auch gefallen