Key Aspect 1 of XSLT writing (learning notes)

Source: Internet
Author: User
Tags xslt xslt processor

XSLT is something similar to CSS. It mainly converts XML into a style sheet output in HTML format (I have not understood it yet ).

To output the content in XML in HTML format, you must create a template in XSLT to set the data format or display style in XML.

When processing An XSLT style table, the XSLT processor first defines the "/" root template for processing, if the template for processing the root element is not defined in the XSLT style sheet, the processor processes the following template. All nodes other than the node range defined in this template are ignored and not processed.

Example:

A. xml

<Root>

<AAA id = "A1">

<BBB id = "B1"/>

<BBB id = "B2"/>

</AAA>

<AAA id = "A2">

<BBB id = "B3"/>

<BBB id = "B4"/>

<CCC id = "C1">

<DDD id = "d1"/>

</CCC>

<BBB id = "B5">

<CCC id = "C2"/>

</BBB>

</AAA>

</Root>

XSLT File

<XSL: stylesheet version = '1. 0'

Xmlns: XSL = 'HTTP: // www.w3.org/5o/#/transform'>

<XSL: template match = "AAA">

<Div style = "color: Purple">

<XSL: value-of select = "Name ()"/>

<XSL: Text> id = </XSL: Text>

<XSL: value-of select = "@ ID"/>

</Div>

</XSL: Template>

<XSL: template match = "BBB">

<Div style = "color: Blue">

<XSL: value-of select = "Name ()"/>

<XSL: Text> id = </XSL: Text>

<XSL: value-of select = "@ ID"/>

</Div>

</XSL: Template>

<XSL: template match = "CCC">

<Div style = "color: maroon">

<XSL: value-of select = "Name ()"/>

<XSL: Text> id = </XSL: Text>

<XSL: value-of select = "@ ID"/>

</Div>

</XSL: Template>

<XSL: template match = "DDD">

<Div style = "color: Green">

<XSL: value-of select = "Name ()"/>

<XSL: Text> id = </XSL: Text>

<XSL: value-of select = "@ ID"/>

</Div>

</XSL: Template>


</XSL: stylesheet>

Output result:

<Div style = "color: Purple"> AAA id = A1 </div>

<Div style = "color: Purple"> AAA id = a2 </div>

Because the root template "/" is not defined in XSLT, the XSLT processor only uses the following template, and does not process any node other than the template used.

If an XSLT style table with a defined root "/" template is used, the effect will be completely different, and the processor will process all the defined nodes.

XSLT style sheet

<XSL: stylesheet version = '1. 0'

Xmlns: XSL = 'HTTP: // www.w3.org/5o/#/transform'>

<XSL: template match = "/">

<XSL: Apply-templates/>

</XSL: Template>

<XSL: template match = "/effectutorial">

<XSL: Apply-templates/>

</XSL: Template>

<XSL: template match = "AAA">

<Div style = "color: Purple">

<XSL: value-of select = "Name ()"/>

<XSL: Text> id = </XSL: Text>

<XSL: value-of select = "@ ID"/>

</Div>

<XSL: Apply-templates/>

</XSL: Template>




id =






id =



<XSL: template match = "DDD">

<Div style = "color: Green">

<XSL: value-of select = "Name ()"/>

<XSL: Text> id = </XSL: Text>

<XSL: value-of select = "@ ID"/>

</Div>

</XSL: Template>


</XSL: stylesheet>

Output results:

<Div style = "color: Purple"> AAA id = A1 </div>

<Div style = "color: Blue"> BBB id = b1 </div>

<Div style = "color: Blue"> BBB id = b2 </div>


<Div style = "color: Purple"> AAA id = a2 </div>

<Div style = "color: Blue"> BBB id = B3 </div>

<Div style = "color: Blue"> BBB id = B4 </div>

<Div style = "color: maroon"> CCC id = C1 </div>

<Div style = "color: Green"> DDD id = D1 </div>


<Div style = "color: Blue"> BBB id = B5 </div>

<Div style = "color: maroon"> CCC id = c2 </div>

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.