XML/XSLT stylesheet/Namespace

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

Deal with the node with namespace

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>
        <h3>
          Namespace nodes of the myNamespace:Book element.
        </h3>
        <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
        <b>
          <xsl:value-of select="name(.)" />
        </b>
        has the namespace URI
        <b>
          <xsl:value-of select="." />
        </b>
        .
      </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>
      <h3>
                   Namespace nodes of the myNamespace:Book element.
                 
      </h3>
      <paragraph>1
                 . The namespace prefix
                 <b>xml</b>
                 has the namespace URI
                 <b>http://www.w3.org/XML/1998/namespace</b>
                 .
               
      </paragraph>
      <paragraph>2
                 . The namespace prefix
                 <b>myNamespace</b>
                 has the namespace URI
                 <b>http://www.wbex.ru/namespaces</b>
                 .
               
      </paragraph>
   </body>
</html>



Namespace alias

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>



namespace axis

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