XML/XSLT stylesheet/copy of

Материал из Web эксперт
Перейти к: навигация, поиск

Copy all the attributes and other nodes

   <source lang="xml">

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:template match="year">
   <vintage>
     <xsl:apply-templates />
   </vintage>
 </xsl:template>
 <xsl:template match="price"></xsl:template>
 <xsl:template match="@*|node()">
   <xsl:copy>
     <xsl:apply-templates select="@*|node()" />
   </xsl:copy>
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><wine grape="chardonnay">

 <product>product 2</product>
 <vintage>1997</vintage>
 

</wine>

</source>
   
  


Copy all the other source tree nodes

   <source lang="xml">

File: Data.xml <wine price="10.99" year="1997">product 2</wine> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:template match="@*|node()">
   <xsl:copy>
     <xsl:apply-templates select="@*|node()" />
   </xsl:copy>
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><wine price="10.99" year="1997">product 2</wine>

</source>
   
  


copy-of select="."

   <source lang="xml">

File: Data.xml <employeeData>

 <employee fileLoc="2.xml"/>
 <employee fileLoc="3.xml"/>
 <employee fileLoc="1.xml"/>

</employeeData> 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="/">
   <employeeData>
     <title>Master Employee Data File</title>
     <xsl:for-each select="employeeData/employee">
       <xsl:apply-templates/>
     </xsl:for-each>
   </employeeData>
 </xsl:template>
 <xsl:template match="employee">
   <xsl:copy-of select="." />
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <employeeData>

  <title>Master Employee Data File</title>

</employeeData>

</source>
   
  


Copy value

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <PurchaseOrder>

 <From>a</From>
 <To>b</To>
 <Address>
   <Street>Street</Street>
   <City>Town</City>
   <State>MO</State>
   <ZipCode>11111</ZipCode>
 </Address>

</PurchaseOrder>

File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:template match="/">
   <Invoice>
     <xsl:apply-templates select="PurchaseOrder/To" />
     <xsl:apply-templates select="PurchaseOrder/From" />
     <xsl:apply-templates select="PurchaseOrder/Address" />
     <xsl:comment>
       The rest of the Invoice would go here.
     </xsl:comment>
   </Invoice>
 </xsl:template>
 <xsl:template match="To">
   <xsl:element name="From">
     <xsl:value-of select="." />
   </xsl:element>
 </xsl:template>
 <xsl:template match="From">
   <xsl:element name="To">
     <xsl:value-of select="." />
   </xsl:element>
 </xsl:template>
 <xsl:template match="Address">
   <xsl:copy-of select="." />
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><Invoice><From>b</From><To>a</To><Address>

   <Street>Street</Street>
   <City>Town</City>
   <State>MO</State>
   <ZipCode>11111</ZipCode>
 </Address></Invoice>
</source>