XML Tutorial/XSLT stylesheet/copy
Версия от 21:22, 25 мая 2010; (обсуждение)
Copy element copies only the current node without children and attributes, while copy-of copies everything.
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
Compare these constructs.
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">
<xsl:text>copy-of : </xsl:text> <xsl:copy-of select="."/>
<xsl:text>copy : </xsl:text> <xsl:copy/>
<xsl:text>value-of : </xsl:text> <xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet> Output:
<?xml version="1.0" encoding="UTF-8"?>copy-of :
Compare these constructs.
copy : <p/>
value-of :
</source>
Compare these constructs.
The <xsl:copy> Element copies a node to the result tree
<source lang="xml">
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></source>
<xsl:copy> and <xsl:copy-of>
<source lang="xml">
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></source>