XML Tutorial/XSLT stylesheet/copy

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

Copy element copies only the current node without children and attributes, while copy-of copies everything.

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<p id="a12">
    Compare <B>these constructs</B>.
</p>
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="p">
      <DIV>
        <B>
          <xsl:text>copy-of : </xsl:text>
        </B>
        <xsl:copy-of select="."/>
      </DIV>
      <DIV>
        <B>
          <xsl:text>copy : </xsl:text>
        </B>
        <xsl:copy/>
      </DIV>
      <DIV>
        <B>
          <xsl:text>value-of : </xsl:text>
        </B>
        <xsl:value-of select="."/>
      </DIV>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><DIV><B>copy-of : </B><p id="a12">
    Compare <B>these constructs</B>.
</p></DIV><DIV><B>copy : </B><p/></DIV><DIV><B>value-of : </B>
    Compare these constructs.
</DIV>


The <xsl:copy> Element copies a node to the result tree

File: Data.xml
<wine grape="Cabernet">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>2008</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">
    <xsl:copy>
      <xsl:attribute name="date">
               <xsl:value-of select="@date" />
            </xsl:attribute>
      <xsl:attribute name="vendor">
               <xsl:text>Snee Wines</xsl:text>
            </xsl:attribute>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="winery | product | year" />
</xsl:stylesheet>
Output:

  
  
  
  <prices date="12/1/01" vendor="Snee Wines">
    13.99
    11.00
  </prices>


<xsl:copy> and <xsl:copy-of>

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<eu>
 <member>
  <state>Austria</state>
  <state founding="yes">Belgium</state>
 </member>
 <candidate>
  <state>Bulgaria</state>
  <state>Cyprus</state>
  <state>Czech Republic</state>
 </candidate>
</eu>

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="eu">
    <xsl:copy>
      <xsl:apply-templates select="candidate" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="candidate">
    <xsl:copy>
      <xsl:copy-of select="state" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<eu>
   <candidate>
      <state>Bulgaria</state>
      <state>Cyprus</state>
      <state>Czech Republic</state>
   </candidate>
</eu>