XML Tutorial/XSLT stylesheet/document

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

select all document

File: Data.xml

<?xml version="1.0" encoding="utf-8"?>
<emailList>
  <head:header xmlns:head="http://www.domain.ru/namespace/header">
    <title>Email List</title>
    <maintainer>Joe</maintainer>
  </head:header>
  <person type="personal" id="p001">
    <name>person1</name>
    <email>p@hotmail.ru</email>
  </person>
  <person type="work" id="p002">
    <name>person2</name>
    <email>p@hotmail.ru</email>
  </person>
  <person type="personal" id="p003">
    <name>person3</name>
    <email>p3@hotmail.ru</email>
  </person>
</emailList>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:head="http://www.domain.ru/namespace/header">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <paragraph>
      Additional Information about the employee:
      <br />
      <xsl:copy-of select="document("Data.xml")" />
    </p>
  </xsl:template>
</xsl:stylesheet>


select=document(capitals.xml)/capitals

File: Data.xml
<?xml version="1.0" encoding="US-ASCII"?>
<usstates>
 <western>
  <usstate>Arizona</usstate>
  <usstate>California</usstate>
  <usstate>Idaho</usstate>
  <usstate>Montana</usstate>
  <usstate>Nevada</usstate>
  <usstate>Oregon</usstate>
  <usstate>Washington</usstate>
  <usstate>Utah</usstate>
 </western>
</usstates>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:key name="Capital" match="capital" use="@usstate" />
  <xsl:key name="State" match="usstate" use="text()" />
  <xsl:param name="cr">Arizona</xsl:param>
  <xsl:template match="/">
    <xsl:apply-templates select="document("capitals.xml")/capitals" />
    <xsl:text>, </xsl:text>
    <xsl:value-of select="key("State", $cr)" />
  </xsl:template>
  <xsl:template match="capitals">
    <xsl:value-of select="key("Capital", $cr)" />
  </xsl:template>
</xsl:stylesheet>


select="document(@location)"

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<volume name="Old Testament">
  <book name="Java">
    <chapter location="Java1.xml" />
    <chapter location="Java2.xml" />
    <chapter location="Java3.xml" />
    <chapter location="Java4.xml" />
  </book>
</volume>
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="volume">
    <xsl:copy>
      <xsl:attribute name="name"><xsl:value-of select="@name" />
      </xsl:attribute>
      <xsl:apply-templates select="book" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="book">
    <xsl:copy>
      <xsl:attribute name="name"><xsl:value-of select="@name" />
      </xsl:attribute>
      <xsl:apply-templates select="chapter" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="chapter">
    <xsl:copy-of select="document(@location)" />
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<volume name="Old Testament">
   <book name="Java"/>
</volume>


Use document to load an XML file

File: Data.xml
<?xml version="1.0"?>
<chapter number="1">
  <verse number="1">line 1</verse>
  <verse number="2">line 2</verse>
  <verse number="3">line 3</verse>
</chapter>

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="volume">
 <xsl:copy>
  <xsl:attribute name="name">Old Testament</xsl:attribute>
  <xsl:apply-templates select="book"/>
 </xsl:copy>
</xsl:template>
<xsl:template match="book">
 <xsl:copy>
  <xsl:attribute name="name">Java</xsl:attribute>
  <xsl:copy-of select="document("a.xml")"/>
 </xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  line 1
  line 2
  line 3