XML/XSLT stylesheet/context

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

Relative context

File: Data.xml
<book>
  <title>title 1</title>
  <chapter>
    <title>chapter 1</title>
    <para>line 1</para>
    <para>line 2</para>
  </chapter>
  <chapter>
    <title>title 2</title>
    <para>line 3</para>
    <para>line 4</para>
  </chapter>
</book>
File: Transform.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="title">
    Title:
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="chapter/title">
    Chapter title:
    <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  
    Title:
    title 1
  
    
    Chapter title:
    chapter 1
    line 1
    line 2
  
  
    
    Chapter title:
    title 2
    line 3
    line 4



Relative context from root element

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/year">
    <vintage>
      <xsl:apply-templates />
    </vintage>
  </xsl:template>
</xsl:stylesheet>
Output:
  product 2
  <vintage>1997</vintage>
  10.99