XML/XSLT stylesheet/Namespace

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

Deal with the node with namespace

   <source lang="xml">

File: Data.xml <myNamespace:Book xmlns:myNamespace="http://www.wbex.ru/namespaces">

<myNamespace:Chapter number="1">chapter 1</myNamespace:Chapter>
<myNamespace:Chapter number="2">chapter 2</myNamespace:Chapter>

</myNamespace:Book> File: Transform.xslt <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:myNamespace="http://www.wbex.ru/namespaces">
 <xsl:template match="/">
   <html>
     <head>
       <title>This shows namespace nodes.</title>
     </head>
     <body>

Namespace nodes of the myNamespace:Book element.

       <xsl:apply-templates select="/myNamespace:Book" />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="myNamespace:Book">
   <xsl:for-each select="namespace::node()">
     <paragraph>
       <xsl:value-of select="position()" />
       . The namespace prefix
       
         <xsl:value-of select="name(.)" />
       
       has the namespace URI
       
         <xsl:value-of select="." />
       
       .
     </paragraph>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet> Output: <html xmlns:myNamespace="http://www.wbex.ru/namespaces">

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>This shows namespace nodes.</title>
  </head>
  <body>

Namespace nodes of the myNamespace:Book element.

     <paragraph>1
                . The namespace prefix
                xml
                has the namespace URI
                http://www.w3.org/XML/1998/namespace
                .
              
     </paragraph>
     <paragraph>2
                . The namespace prefix
                myNamespace
                has the namespace URI
                http://www.wbex.ru/namespaces
                .
              
     </paragraph>
  </body>

</html>

</source>
   
  


Namespace alias

   <source lang="xml">

File: Data.xml <test xmlns:snee="http://www.wbex.ru/dtds/test"

     xmlns:demo2s="http://www.demo2s.ru/dtds/test"
     xmlns:domain="http://www.domain.ru/dtds/test">

this is a test. </test>

File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xslt="output.xsl">
 <xsl:param name="variable-name">v</xsl:param>
 <xsl:param name="default-value" />
 <xsl:output indent="yes" />
 <xsl:namespace-alias stylesheet-prefix="xslt" result-prefix="xsl" />
 <xsl:template match="/" name="main">
   <xslt:stylesheet version="1.0">
     <xslt:variable name="{$variable-name}">
       <xsl:value-of select="$default-value" />
     </xslt:variable>
   </xslt:stylesheet>
 </xsl:template>

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

  <xsl:variable name="v"/>

</xsl:stylesheet>

</source>
   
  


namespace axis

   <source lang="xml">

File: Data.xml <test xmlns:snee="http://www.wbex.ru/dtds/test"

     xmlns:demo2s="http://www.demo2s.ru/dtds/test"
     xmlns:domain="http://www.domain.ru/dtds/test">

this is a test. </test> 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="test">
   <xsl:for-each select="namespace::*">
     <xsl:value-of select="name()" />
     <xsl:text> </xsl:text>
   </xsl:for-each>
 </xsl:template>
 

</xsl:stylesheet> Output:

snee demo2s domain 
</source>