Transforming With XSLT Stylesheets

download Transforming With XSLT Stylesheets

of 5

Transcript of Transforming With XSLT Stylesheets

  • 7/29/2019 Transforming With XSLT Stylesheets

    1/5

    Transforming with XSLT stylesheets

    The standard way to transform XML data into other formats is by ExtensibleStylesheet Language Transformations (XSLT). You can use the built-in

    XSLTRANSFORM function to convert XML documents into HTML, plain text,or different XML schemas.

    XSLT uses stylesheets to convert XML into other data formats. You canconvert part or all of an XML document and select or rearrange the data usingthe XPath query language and the built-in functions of XSLT. XSLT iscommonly used to convert XML to HTML, but can also be used to transformXML documents that comply with one XML schema into documents thatcomply with another schema. XSLT can also be used to convert XML datainto unrelated formats, like comma-delimited text or formatting languages

    such as troff. XSLT has two main areas of applicability: Formatting (conversion of XML into HTML or formatting languages such

    as FOP); Data exchange (querying, reorganizing and converting data from one

    XML schema to another, or into a data exchange format such asSOAP).

    Both cases may require that an entire XML document or only selected parts ofit be transformed. XSLT incorporates the XPath specification, permitting queryand retrieval of arbitrary data from the source XML document. An XSLTtemplate may also contain or create additional information such as file

    headers and instruction blocks that will be added to the output file.How XSLT WorksXSLT stylesheets are written in Extensible Stylesheet Language (XSL), anXML schema. XSL is a template language rather than an algorithmic languagesuch as C or Perl, a feature that limits XSL's power but makes it uniquelysuited to its purpose. XSL stylesheets contain one or

    moretemplate elements, which describe what action to take when a given

    XML element or query is encountered in the target file. A typical XSLTtemplate element will start by specifying which element it applies to. Forinstance,

    declares that the contents of this template will be used to replace the content

    of any tag encountered in the target XML file. An XSLT file

    consists of a list of such templates, in no necessary order.The following example shows typical elements of an XSLT template. In thiscase the target will be XML documents containing inventory information, suchas this record describing an ice scraper:

  • 7/29/2019 Transforming With XSLT Stylesheets

    2/5

    Ice Scraper, Windshield 4 inch

    Basic Ice Scraper 4 inches wide, foam

    handle3.99

    This record includes such information as the part number, description andprice of a windshield ice scraper. Some of this information is contained within

    elements, such as . Some, like the part number, are contained in

    attributes (in this case the pid attribute of the element). To

    display this information as a web page, you could apply the following XSLTtemplate:

    product ID

    product name

  • 7/29/2019 Transforming With XSLT Stylesheets

    3/5

    price

    $

    details

    When an XSLT processor receives as input both the template and targetdocuments above, it will output the following HTML document:

    Ice Scraper, Windshield 4 inch

    product ID100-201-01

    product nameIce Scraper,

    Windshield 4 inch

    price$3.99

    detailsBasic Ice Scraper 4 inches

    wide, foam handle

    The XSLT processor tests the incoming XML document for given conditions(typically one condition per template). If a condition is true the templatecontents are inserted into the output, and if they are false the template is

  • 7/29/2019 Transforming With XSLT Stylesheets

    4/5

    passed over by the processor. The stylesheet may add its own data to theoutput, for example in the HTML table tagging and strings such as "productID."

    XPath can be used both to define template conditions, as in and to select and insert data from anywhere in the XML

    stream, as in .

    Using XSLTRANSFORM

    You can use the XSLTRANSFORM function to apply XSLT stylesheets toXML data. If you supply the function with the name of an XML document andan XSLT stylesheet, the function will apply the stylesheet to the document and

    return the result.

    Passing parameters to XSLT stylesheets at runtimeParameter can be passed at runtime when using the built-inXSLTRANSFORM function to convert XML documents.

    Example: Using XSLT as a formatting engineAn example that illustrates how to use the built-in XSLTRANSFORMfunction as a formatting engine.

    Example: Using XSLT for data exchange

    An example that illustrates how to use the built-in XSLTRANSFORMfunction to convert XML documents for data exchange.

    Example: Using XSLT to remove namespacesXML documents you receive might contain unneeded or incorrectnamespace information. You can use XSLT style sheets to remove ormanipulate the namespace information in the documents.

    Important considerations for transforming XML documentsWhen using the built-in XSLTRANSFORM function to convert XML

    documents some important considerations and restrictions apply.

    Parent topic:Querying XML dataRelated conceptsLesson 8: Transforming with XSLT stylesheets

    Related referenceXSLTRANSFORM scalar function

    http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051502.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051502.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051503.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051503.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051504.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051504.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/r0054369.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/r0054369.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051505.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051505.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0023895.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0023895.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0023895.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0050645.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0050645.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0050650.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0050650.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0050650.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0050645.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0023895.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051505.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/r0054369.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051504.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051503.htmlhttp://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/c0051502.html
  • 7/29/2019 Transforming With XSLT Stylesheets

    5/5

    Concept topic