XML/XSLT stylesheet/xml output

Материал из Web эксперт
Версия от 08:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

achieves in a different and laborious way.

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>    
        <text size="H1">Header1</text>
    <text size="H3">Header3</text>
    <text size="b">Bold text</text>
    <text size="sub">Subscript</text>
    <text size="sup">Superscript</text>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <xsl:for-each select="//text">
        <xsl:choose>
          <xsl:when test="@size="H1"">
            <H1>
              <xsl:value-of select="."/>
            </H1>
          </xsl:when>
          <xsl:when test="@size="H3"">
            <H3>
              <xsl:value-of select="."/>
            </H3>
          </xsl:when>
          <xsl:when test="@size="b"">
            <b>
              <xsl:value-of select="."/>
            </b>
          </xsl:when>
          <xsl:when test="@size="sub"">
            <sub>
              <xsl:value-of select="."/>
            </sub>
          </xsl:when>
          <xsl:when test="@size="sup"">
            <sup>
              <xsl:value-of select="."/>
            </sup>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><H1>Header1</H1><H3>Header3</H3><b>Bold text</b><sub>Subscript</sub><sup>Superscript</sup>



Add 1 hyphen to result tree

File: Data.xml
<sample>test</sample>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template name="hyphens">
    <xsl:param name="howMany">1</xsl:param>
    <xsl:if test="$howMany &gt; 0">
      <xsl:text>-</xsl:text>
      <xsl:call-template name="hyphens">
        <xsl:with-param name="howMany" select="$howMany - 1" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  <xsl:template match="sample">
    Print 1 hyphen:
    <xsl:call-template name="hyphens" />
    Print 3 hyphens:
    <xsl:call-template name="hyphens">
      <xsl:with-param name="howMany" select="3" />
    </xsl:call-template>
    Print 20 hyphens:
    <xsl:call-template name="hyphens">
      <xsl:with-param name="howMany" select="20" />
    </xsl:call-template>
    Print 0 hyphens:
    <xsl:call-template name="hyphens">
      <xsl:with-param name="howMany" select="0" />
    </xsl:call-template>
  </xsl:template>
  
</xsl:stylesheet>
Output:

    Print 1 hyphen:
    -
    Print 3 hyphens:
    ---
    Print 20 hyphens:
    --------------------
    Print 0 hyphens:



Add Comment to generated xml document

File: Data.xml
<?xml version="1.0"?>
<message>You can insert comments in your output.</message>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <xsl:comment>comment &amp; msg element</xsl:comment>
    <msg>
      <xsl:apply-templates />
    </msg>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!--comment & msg element--><msg>You can insert comments in your output.</msg>



Add indenting to make result line up better

File: Data.xml
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  <xsl:template match="figure/title">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="para" />
</xsl:stylesheet>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="chapter">
    Pictures:
    <xsl:for-each select="descendant::figure">
      <xsl:value-of select="title" />
      <xsl:text>
            </xsl:text>
    </xsl:for-each>
    Chapter:
    <xsl:apply-templates />
  </xsl:template>
  
</xsl:stylesheet>



Add new parent tag

File: Data.xml
<customer>
  <last>Hill</last>
  <first>Phil</first>
  <phone>212-555-1212</phone>
  <address>166 Main St.</address>
  <city>New York</city>
  <state>NY</state>
  <zip>10001</zip>
</customer>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  <xsl:template match="customer">
    <client>
      <xsl:apply-templates />
    </client>
  </xsl:template>
  
</xsl:stylesheet>
Output:
<client>
  Hill
  Phil
  212-555-1212
  166 Main St.
  New York
  NY
  10001
</client>



Add processing-instruction to generated xml document

File: Data.xml
<?xml version="1.0"?>
<message>test</message>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <xsl:processing-instruction name="xml-stylesheet">
      href="processing.css" type="text/css"
    </xsl:processing-instruction>
    <xsl:element name="doc">
      <xsl:element name="paragraph">
        <xsl:apply-templates />
      </xsl:element>
    </xsl:element>
  </xsl:template>
  <xsl:template match="courier">
    <xsl:element name="code">
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="processing.css" type="text/css"
    ?><doc>
   <paragraph>test</paragraph>
</doc>



attribute name="example" select="1 to 7" separator=", "

File: Data.xml
<?xml version="1.0"?>
<list xml:lang="en">
  <title>title 1</title>
  <listitem>item 1</listitem>
  <listitem>item 2</listitem>
  <listitem>item 3</listitem>
  <listitem xml:lang="sw">item 4</listitem>
  <listitem xml:lang="en-gb">item 5</listitem>
  <listitem xml:lang="zu">item 6</listitem>
  <listitem xml:lang="jz">item 7</listitem>
</list>

File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <sampledoc>
      <xsl:attribute name="example" select="1 to 7" separator=", "/>
    </sampledoc>
  </xsl:template>
</xsl:stylesheet>
Output:
<sampledoc example="1, 2, 3, 4, 5, 6, 7"/>



Create new element with attribute

File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<message>Message.</message>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <xsl:element name="paragraph">
      <xsl:attribute name="priority">medium</xsl:attribute>
      <xsl:attribute name="date">2003-09-23</xsl:attribute>
      <xsl:attribute name="doc:style"
        namespace="http://www.wbex.ru/documents">classic</xsl:attribute>
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph xmlns:doc="http://www.wbex.ru/documents" priority="medium" date="2003-09-23"
           doc:style="classic">Message.</paragraph>



Create new xml document with namespace

File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<message>Message.</message>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:attribute-set name="paragraph">
    <xsl:attribute name="priority">medium</xsl:attribute>
    <xsl:attribute name="date">2003-09-23</xsl:attribute>
    <xsl:attribute name="doc:style"
      namespace="http://www.wbex.ru/documents">classic</xsl:attribute>
  </xsl:attribute-set>
  <xsl:template match="/">
    <xsl:element name="paragraph" use-attribute-sets="paragraph">
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph xmlns:doc="http://www.wbex.ru/documents" priority="medium" date="2003-09-23"
           doc:style="classic">Message.</paragraph>



Disable escape

File: Data.xml
<?xml version="1.0"?>
<message>test</message>

File: Transform.xslt
<stylesheet version="1.0"
  xmlns="http://www.w3.org/1999/XSL/Transform">
  <output method="xml" omit-xml-declaration="yes" />
  <template match="/">
    <publisher xmlns="">
      <value-of select="title"
        xmlns="http://www.w3.org/1999/XSL/Transform" />
      <text disable-output-escaping="no"
        xmlns="http://www.w3.org/1999/XSL/Transform"> &amp; Associates</text>
    </publisher>
  </template>
</stylesheet>
Output:
<publisher> &amp; Associates</publisher>



disable-output-escaping="yes"

File: Data.xml
<title>wbex</title>
File: Transform.xslt
<stylesheet version="1.0"
  xmlns="http://www.w3.org/1999/XSL/Transform">
  <output method="xml" omit-xml-declaration="yes" />
  <template match="/">
    <publisher xmlns="">
      <value-of select="title"
        xmlns="http://www.w3.org/1999/XSL/Transform" />
      <text disable-output-escaping="yes"
        xmlns="http://www.w3.org/1999/XSL/Transform"> &amp; Associates</text>
    </publisher>
  </template>
</stylesheet>
Output:
<publisher>wbex & Associates</publisher>



output doctype-system="testOut.dtd"

File: Data.xml
<test>test</test>
File: Transform.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output doctype-system="testOut.dtd" />
  <xsl:template match="test">
    <testOut>
      test.
      <xsl:apply-templates />
    </testOut>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE testOut
  SYSTEM "testOut.dtd">
<testOut>
      test.
      test</testOut>



Output element with Namespace

File: Data.xml
<?xml version="1.0"?>
<message>test</message>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <xsl:element name="doc:paragraph"
      namespace="http://www.wbex.ru/documents">
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<doc:paragraph xmlns:doc="http://www.wbex.ru/documents">test</doc:paragraph>



output method="xml" omit-xml-declaration="yes"

File: Data.xml

<test>test</test>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes"/>
</xsl:stylesheet>
Output:
test



output method="xml" version="1.1" encoding="utf-16"

File: Data.xml

<test>test</test>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" version="1.1" encoding="utf-16" />
</xsl:stylesheet>



Output new xml tag

File: Data.xml

<employees>
  <employee hireDate="04/23/1999" officer="yes">
    <last>A</last>
    <first>B</first>
    <salary>100000</salary>
  </employee>
  <employee hireDate="09/01/1998" officer="no">
    <last>C</last>
    <first>D</first>
    <salary>95000</salary>
  </employee>
  <employee hireDate="08/20/2000">
    <last>E</last>
    <first>F</first>
    <salary>89000</salary>
  </employee>
</employees>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="employee">
    <client>
      <xsl:apply-templates />
    </client>
  </xsl:template>
</xsl:stylesheet>
Output:

  <client>
    A
    B
    100000
  </client>
  <client>
    C
    D
    95000
  </client>
  <client>
    E
    F
    89000
  </client>



Output xml element in various form

File: Data.xml
<test>test</test>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="test">
    <test>
      1.
      <sample />
      2.
      <sample></sample>
      3.
      <xsl:element name="sample" />
      4.
      <sample color="green" />
      5.
      <sample color="green"></sample>
      6.
      <xsl:element name="sample">
        <xsl:attribute name="color">green</xsl:attribute>
      </xsl:element>
      7.
      <sample></sample>
      <xsl:apply-templates />
    </test>
  </xsl:template>
  
</xsl:stylesheet>
Output:
<test>
      1.
      <sample/>
      2.
      <sample/>
      3.
      <sample/>
      4.
      <sample color="green"/>
      5.
      <sample color="green"/>
      6.
      <sample color="green"/>
      7.
      <sample/>test</test>



Rename tag

File: Data.xml
<name>
  <last>A</last>
  <first>B</first>
</name>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="name">
    <name>
      <family>
        <xsl:apply-templates select="last" />
      </family>
      <given>
        <xsl:apply-templates select="first" />
      </given>
    </name>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><name><family>A</family><given>B</given></name>



Restructure xml document

File: Data.xml
<wine grape="Chardonnay">
  <product>product 2</product>
  <year>1997</year>
  <price>10.99</price>
</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="wine">
    <wine vintage="{year}">
      <product>
        <xsl:apply-templates select="product" />
      </product>
      <category>
        <xsl:value-of select="@grape" />
      </category>
      <price>
        <xsl:apply-templates select="price" />
      </price>
    </wine>
  </xsl:template>
</xsl:stylesheet>
Output:
<wine vintage="1997"><product>product 2</product><category>Chardonnay</category><price>10.99</price></wine>



Set Encoding

File: Data.xml
<name>
  <last>A</last>
  <first>B</first>
</name>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:output encoding="UTF-16" />
  <xsl:template match="name">
    <name>
      <family>
        <xsl:apply-templates select="last" />
      </family>
      <given>
        <xsl:apply-templates select="first" />
      </given>
    </name>
  </xsl:template>
</xsl:stylesheet>



Use attribute value to create new attribute

File: Data.xml
<wine grape="Cabernet">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>1996</year>
  <prices date="12/1/01">
    <list>13.99</list>
    <discounted>11.00</discounted>
  </prices>
</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="prices">
    <prices date="{@date}">
      <xsl:apply-templates />
    </prices>
  </xsl:template>
  
</xsl:stylesheet>
Output:

  shop 1
  product 1
  1996
  <prices date="12/1/01">
    13.99
    11.00
  </prices>



Use concat function to create name of Element

File: Data.xml
<?xml version="1.0"?>
<message>
  You can use the element element to create elements on the result
  tree.
</message>

File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="message">
    <xsl:element name="{concat("my", name())}">
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<mymessage>
  You can use the element element to create elements on the result
  tree.
</mymessage>



use for-each to output xml tags

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<emailList>
  <person>
    <name>person1</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person2</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person3</name>
    <email>p3@hotmail.ru</email>
  </person>
</emailList>


File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
          version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <contacts>
      <xsl:for-each select="emailList/person">
        <contact>
          <fullName>
            <xsl:value-of select="name"/>
          </fullName>
          <eMail>
            <xsl:value-of select="email"/>
          </eMail>
        </contact>
      </xsl:for-each>
    </contacts>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<contacts>
   <contact>
      <fullName>person1</fullName>
      <eMail>p@hotmail.ru</eMail>
   </contact>
   <contact>
      <fullName>person2</fullName>
      <eMail>p@hotmail.ru</eMail>
   </contact>
   <contact>
      <fullName>person3</fullName>
      <eMail>p3@hotmail.ru</eMail>
   </contact>
</contacts>



use new tag to wrap

File: Data.xml
<?xml version="1.0"?>
<message>test</message>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <paragraph>
      <xsl:apply-templates />
    </paragraph>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph>test</paragraph>



Using xsl:element and xsl:attribute

File: Data.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<sports>
  <game title="cricket">
    <id>243</id>
    <paragraph>para 1</paragraph>
  </game>

</sports>
File: Transform.xslt
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- match sports elements -->
  <xsl:template match="/sports">
    <sports>
      <xsl:apply-templates select="game" />
    </sports>
  </xsl:template>
  <!-- match game elements -->
  <xsl:template match="game">
    <!-- create child element -->
    <xsl:element name="{@title}">
      <!-- create attribute -->
      <xsl:attribute name="id">
            <xsl:value-of select="id" />
         </xsl:attribute>
      <comment>
        <xsl:value-of select="paragraph" />
      </comment>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><sports><cricket id="243"><comment>para 1</comment></cricket></sports>



Using xsl:element and xsl:attribute to create xml tags

File: Data.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<sports>
   <game title = "cricket">
      <id>243</id>
      <paragraph>
         para 1
      </paragraph>
   </game>

</sports>
File: Transform.xslt

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" 
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
   
   <!-- match sports elements -->
   <xsl:template match = "/sports">
      <sports>
         <xsl:apply-templates select = "game"/>
      </sports>
   </xsl:template>
   <!-- match game elements -->
   <xsl:template match = "game">
      <!-- create child element -->
      <xsl:element name = "{@title}">
         <!-- create attribute -->
         <xsl:attribute name = "id">
            <xsl:value-of select = "id"/>
         </xsl:attribute>
         <comment>
            <xsl:value-of select = "paragraph"/>
         </comment>
      </xsl:element> 
   </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><sports><cricket id="243"><comment>
         para 1
      </comment></cricket></sports>



xsl:attribute generates elements

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>  
        <color>blue</color>
    <color>navy</color>
    <color>green</color>
    <color>lime</color>
    <color>red</color>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="color">
      <TABLE>
        <TR>
          <TD>
            <xsl:attribute name="style"><xsl:text>color:</xsl:text><xsl:value-of select="."/></xsl:attribute>
            <xsl:value-of select="."/>
          </TD>
        </TR>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>  
        <TABLE><TR><TD style="color:blue">blue</TD></TR></TABLE>
    <TABLE><TR><TD style="color:navy">navy</TD></TR></TABLE>
    <TABLE><TR><TD style="color:green">green</TD></TR></TABLE>
    <TABLE><TR><TD style="color:lime">lime</TD></TR></TABLE>
    <TABLE><TR><TD style="color:red">red</TD></TR></TABLE>



xsl:element generates elements in time of processing.

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>    
        <text size="H1">Header1</text>
    <text size="H3">Header3</text>
    <text size="b">Bold text</text>
    <text size="sub">Subscript</text>
    <text size="sup">Superscript</text>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <xsl:for-each select="//text">
        <xsl:element name="{@size}">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><H1>Header1</H1><H3>Header3</H3><b>Bold text</b><sub>Subscript</sub><sup>Superscript</sup>