XPath and XSL Conversion -- apply XSL to XML

Source: Internet
Author: User
Tags xslt xslt processor

This example illustrates how to use the Extensible transform class to apply an XSLT file to an XML document. This class implements the W3C XSL conversion (XSLT) Version 1.0 specification of the WWW Federation (W3C. (For more information, see W3C W3C extensible style sheet language (XSL) 1.0 specifications .)


VB transformxml. aspx

[Running example] | [View Source code]

XSLT is an XML-based language used to convert an XML document to another XML document or
Convert a document to another structured document. XSLT uses XML Path Language (XPath) in XML
To specify a specific part of the document. (For examples of how to use XPath to query XML documents, see how to use XPath expressions to query XML documents .)

The structure of an XML document often does not match the structure required to process XML data. To convert an existing structure to a processing structure, you need to use XSLT
The processor and the XSLT style sheet (or XSLT file) that defines how to convert ). An XSLT file consists of templates that specify the source XML
How should each node in the document appear in the target XML document. Therefore, the XSLT processor has three types of trees:

  1. One is the source XML tree.
  2. One is the target structure tree.
  3. One is the XSLT file tree.

    The following diagram illustrates the conversion process: receive an XML document, apply the XSLT file to the node of the XML document, and generate another document in the required format.

    This example consists of two functions. The first function reads only XML documents and then converts data using XSLT files. The second function reads the XML document, converts the data, and then writes a New XML document with the converted data.

    This example applies the two functions to three groups of documents:

      1. The
        A group of documents are XML documents books. xml and XSLT files books. XSL. XSLT
        A file is composed of templates that obtain all books in the bookstore element, and then obtain the ISBN attribute and price element values for each book element. There is also a template that instructs XSLT
        The processor starts processing from the root ("/") node.
      2. The second set of documents is the XML documents processparametersa. xml and XSLT files.
        Stylesheetgenerator. XSL. In this case, this example reads the XML document to obtain the information to be included in XSLT.
        File. Then, this example generates the specified style table.
      3. The third group of documents is the XML documents processparametersb. XML and the XSLT file stylesheetgenerator. XSL. This example processes these documents in the same way as the second document. However, in this case, different style sheets are generated.

    ColumnCodeDisplays the first of the two functions. This function only reads and converts data. Therefore, this function first creates an xpathdocument object and
    Transform object. Then, load the XSLT file to the xsltransform using the load method.
    Object. To start the conversion, this function calls the transform method passed to the xpathdocument. The conversion process returns
    Xmlreader, used to read converted data.

    Note: As described in how to use XPath expressions to query XML, the xpathdocument class provides a fast and high-performance cache for processing XML documents using XSLT.

     // ARGs [0] is the XML file and ARGs [1] is the stylesheet file. 
    Public void readtransform (string [] ARGs)
    {< br> try
    {< br> xpathdocument myxpathdocument = new xpathdocument (ARGs [0]);
    using transform my‑transform = new using transform ();
    my‑transform. load (ARGs [1]);
    xmlreader reader = my‑transform. transform (myxpathdocument, null);
    formatxml (Reader);
    }< br> catch (exception e)
    {< br> console. writeline ("exception: {0}", E. tostring ();
    }< BR >}< br>
    'Args [0] is the XML file and ARGs [1] is the stylesheet file. public sub readtransform (ARGs as string () Try Dim myxpathdocument as xpathdocument = new xpathdocument (ARGs (0) dim my‑transform as new ‑transform = new ‑transform () my‑transform. load (ARGs (1) dim reader as xmlreader = my‑transform. transform (myxpathdocument, nothing) formatxml (Reader) catch e as exception console. writeline ("exception: {0}", E. tostring () end try end sub
    C # VB  

    In addition to returning xmlreader from the conversion, xmlwriter may also be provided to transform
    Method. A writer is provided to write converted data to a file or stream. In this example, the second of the two functions shows how to convert xmlwriter
    Including in the conversion process. The following code shows how the second function writes converted data to a file named transform. xml and then outputs the file to the console.

    // ARGs [0] is the XML file and ARGs [1] is the stylesheet file.
    Public void readtransformwrite (string [] ARGs)
    Streamreader stream = NULL;
    Xpathdocument myxpathdocument = new xpathdocument (ARGs [0]);
    Using transform my‑transform = new using transform ();
    Xmltextwriter writer = new xmltextwriter ("transform. xml", null );
    My‑transform. Load (ARGs [1]);
    My‑transform. Transform (myxpathdocument, null, writer );
    Writer. Close ();
    Stream = new streamreader ("transform. xml ");
    Console. Write (stream. readtoend ());
    Catch (exception E)
    Console. writeline ("exception: {0}", E. tostring ());
    If (stream! = NULL)
    Stream. Close ();
    'Args [0] is the XML file and ARGs [1] is the stylesheet file. public sub readtransformwrite (ARGs as string () dim stream as streamreader = nothing Try Dim myxpathdocument as xpathdocument = new xpathdocument (ARGs (0) dim my‑transform as primary transform = new primary transform () dim writer as xmltextwriter = new xmltextwriter ("transform. XML ", nothing) my‑transform. load (ARGs (1) my‑transform. transform (myxpathdocument, nothing, writer) writer. close () stream = new streamreader ("transform. XML ") console. write (stream. readtoend () catch e as exception console. writeline ("exception: {0}", E. tostring () Finally if not stream is nothing stream. close () end if end try end sub
    C # VB  

    However, it is not shown in this example, but you can use the Transform Method of terratransform to load the document and directly write it into the file. This is a load from a file
    An easy way to use XML documents, apply XSLT files, and write output to files. In this way, you do not need to create and load the Input Source Document, and then write the file stream. Use this method
    Xpathdocument converts XML, as shown in the following code.

    Transform transform = new transform ();
    Using transform. Load ("favorite. XSL ");
    Using transform. Transform ("mydocument. xml", "transformresult. xml ");
    Dim transform as Extract Transform = new transform () transform. Load ("favorite. XSL") transform. Transform ("mydocument. xml", "transformresult. xml ")
    C # VB  
      1. XSLT is a language that uses XPath as the query language to convert XML documents into other XML documents.
      2. you can use the load method of the transform class to load An XSLT style table (or an XSLT file ).
      3. the Transform Method of the transform class uses xpathdocument and returns an xmlreader, or writes the conversion to the provided xmlwriter or stream.
      4. the volume transform class can convert any data that implements the xpathnavigator and ixpathnavigable interfaces.
      5. you can combine XSLT files to generate the desired structure output format (such as XML or HTML ).
      6. the XSLT processor does not operate on compositions . Their operation document structure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.