xml雜碎知識

 1。Xpath認可4種基本的數據類型:節點集,布爾值,數字,字符串。

最普通的表達式類型就是位置路徑,它由多個單步執行組成,每個步都包含以下3個基本的部分:軸:定義路徑方向;節點測試:選擇特定節點集;謂詞:提供對節點集的測試條件(可選)。位置步的基本語法是:axis::node-test[predicate]

共有13種定義方向的軸:(默認爲child軸)

child按順序選中上下文節點所有的直接子節點,不包括對屬性節點或名稱空間節點的選擇

employees/child::employee/child::*

descendant選擇當前節點的所有後代,允許選擇屬性節點或名稱空間節點

employees/child::employee/child::contact/descendant::*

parent只選擇當前節點的直接父節點//*/parent::node()/tilte

ancestor按文檔順序的相反方向選擇當前節點的所有祖先節點,始終包括根節點

employees/employee/contact/name/ancestor::*

following-sibling按文檔順序選擇文檔中此後出現的當前節點的所有兄弟節點

employees/employee/following-sibling::*/contact/name

preceding-silbling按與文檔順序相反的方向選擇文檔中此前出現的當前節點的所有兄弟節點

employees/employee/preceding-sibling::*

following除當前節點的所有後代節點外,按順序選擇文檔中當前節點之後出現的所有節點,不包括屬性節點或名稱空間節點.employees/employee/following::*

preceding按與文檔方向順序相反的方向選擇文檔中在當前節點之前出現的所有節點。employees/employee/preceding::*

 

attribute選擇當前節點的所有屬性節點employees/employee/attribute::*

namespace選擇當前節點的所有名稱空間節點,若無則該軸爲空

employees/head:header/namespace::*

self選擇當前節點employees/employee/contact/email/self::*

descendant-or-self按文檔順序選擇當前節點及其所有後代節點,不包括屬性節點或名稱空間節點employees/child::employee/child::contact/descendant-or-self::*

ancestor-or-self按文檔順序選擇當前節點及其所有祖先節點

employees/employee/contact/name/ancestor-or-self::*

 

節點測試允許從指定軸選擇具體的元素或節點類型,方法有

node-name用於選擇它自身的節點名稱

*用於選擇任意元素節點的通配符

comment()選擇註釋節點

node()選擇任意類型的節點

precessing-instruction()選擇處理指令節點

text()選擇文本節點

謂詞是一種布爾表達式,允許進一步過濾由軸和節點集確定的節點集

 

縮寫:

node-name(等效:child::node-name)

@name(等效:attribute:name)

.(等效:self::node())

..(等效:parent:node())

//(等效:/descendant-or-self:node()/)

2。

xslt中使用javascript(注意的是腳本中若有――字符,則應該把該代碼從XSLT中取出,放入一個獨立的腳本文件中)

<?xml version=”1.0”?>

<xsl:stylesheet version=”1.0” xmlns:xsl=http://www.w3.org/1999/xsl/transform >

  <xsl:output method=”html” indent=”yes” encoding=”UTF-8” />

  <xsl:template match=”/”>

    <html>

      <head>

<script language=”javaScript”>

<xsl:comment>

      var varName=’pull’;

      var NS4=(document.layers)?1:0;

      var IE=(document.all)?1:0;

      var DOM=(parseInt(navigator.appVersion)&gt;=5)?1:0;

      var MAC=((navigator.appVersion.indexOf(“PPC”)&gt;0)||

(navigator.appVersion.indexOf(“Mac”)&gt;0))?1:0

      function showLayer(){return;}

      function HideLayer(){return;}

      if(document.layers){

         appVer=navigator.appVersion.substring(0,4);

         if(appVer&lt;4.06)NS4=0;

      }

      if(NS4||IE||DOM)pullDown=1;

      if(!pullDown)event=null;

//</xsl:comment>

</script>

    <title>creating a javascript</title>

    </head>

    <body>

     some text here!

    </body>

    </html>

  </xsl:template>

</xsl:stylesheet>

或者可以這樣:

<script language=”javascript”>

<xsl:attribute name=”src”>myscript.js</xsl:attribute>

<xsl:comment>comment for browsers having trouble with script element</xsl:comment>

</script>

 

3。

xsl中的一些指令

xsl:apply-imports引入模板

xsl:apply-templates識別待處理的節點集,調用相應的模板規則

xsl:attribute創建屬性

xsl:call-template調用一個指定的模板

xsl:choose必有一個xsl:when用於選擇

xsl:comment 創建註釋

xsl:copy淺複製

xsl:copy-of深複製

xsl:element創建元素

xsl:fallback用來確保正向兼容

xsl:for-each利用XPath表達式選擇節點集

xsl:if滿足條件時實例化該模板實體

xsl:message輸出一個消息

xsl:mumber插入一個格式化整數

xsl:processing-instruction創建一個處理指令

xsl:text計算xslt結果樹中的文本節點

xsl:value-of將單個源節點的字符串複製給輸出樹

xsl:variable可用做最高級元素或模板中的指令

'''''''''''''''''''''''''''''''''''''''

 

例子:

 

xsl生成超鏈接:

<a><xsl:attribute name="href">Name.asp</xsl:attribute><xsl:value-of select="name" /></a>

指令:

<xsl:processing-instruction name=”xml-stylesheet”>href=”employees.css”

  type=”text/css”</xsl:processing-instruction>

結果:<?xml-stylesheet href=”employees.css” type=”text/css” ?>

創建文字正文:

<xsl:text>some content would be nice</xsl:text>

 

 

應用xsl:for-each:

<xsl:for-each select=”*”>

  <xsl:choose>

    <xsl:when test=”name(parent::*)=’name’”>

       <xsl:value-of select=”.” />

       <xsl:text> </xsl:text>

    </xsl:when>

    <xsl:otherwise>

       <xsl:choose>

         <xsl:when test=”name()=’street’”>

            <xsl:value-of select=”.” />

            <br />

         </xsl:when>

         <xsl:when test=”name()=’city’”>

            <xsl:value-of select=”.” />

            <xsl:text>,</xsl:text>

         <xsl:when>

         <xsl:otherwise>

         <xsl:value-of select=”.” />

         <xsl:text>,</xsl:text>

         </xsl:otherwise>

       </xsl:choose>

    </xsl:otherwise>

   </xsl:choose>

 <xsl:for-each>

    

使用mode屬性促成多路處理:

<xsl:template match=”/”>

    <xsl:apply-templates select=”employees/employee” mode=”programming” />

</xsl:template>

 

<xsl:template match=”employee” mode=”programming”>

    <xsl:if test=”@dept=’programming’”>

    <xsl:value-of select=”contact/name” />

    </xsl:if>

</xsl:template>

 

<xsl:template match=”employee” mode=”training”>

    <xsl:if test=”@dept=’training’”>

    <xsl:value-of select=”contact/name” />

    </xsl:if>

</xsl:template>

xslt中使用javascript(注意的是腳本中若有――字符,則應該把該代碼從XSLT中取出,放入一個獨立的腳本文件中)

<?xml version=”1.0”?>

<xsl:stylesheet version=”1.0” xmlns:xsl=http://www.w3.org/1999/xsl/transform >

  <xsl:output method=”html” indent=”yes” encoding=”UTF-8” />

  <xsl:template match=”/”>

    <html>

      <head>

<script language=”javaScript”>

<xsl:comment>

      var varName=’pull’;

      var NS4=(document.layers)?1:0;

      var IE=(document.all)?1:0;

      var DOM=(parseInt(navigator.appVersion)&gt;=5)?1:0;

      var MAC=((navigator.appVersion.indexOf(“PPC”)&gt;0)||

(navigator.appVersion.indexOf(“Mac”)&gt;0))?1:0

      function showLayer(){return;}

      function HideLayer(){return;}

      if(document.layers){

         appVer=navigator.appVersion.substring(0,4);

         if(appVer&lt;4.06)NS4=0;

      }

      if(NS4||IE||DOM)pullDown=1;

      if(!pullDown)event=null;

//</xsl:comment>

</script>

    <title>creating a javascript</title>

    </head>

    <body>

     some text here!

    </body>

    </html>

  </xsl:template>

</xsl:stylesheet>

或者可以這樣:

<script language=”javascript”>

<xsl:attribute name=”src”>myscript.js</xsl:attribute>

<xsl:comment>comment for browsers having trouble with script element</xsl:comment>

</script>

 

3。

xsl中的一些指令

xsl:apply-imports引入模板

xsl:apply-templates識別待處理的節點集,調用相應的模板規則

xsl:attribute創建屬性

xsl:call-template調用一個指定的模板

xsl:choose必有一個xsl:when用於選擇

xsl:comment 創建註釋

xsl:copy淺複製

xsl:copy-of深複製

xsl:element創建元素

xsl:fallback用來確保正向兼容

xsl:for-each利用XPath表達式選擇節點集

xsl:if滿足條件時實例化該模板實體

xsl:message輸出一個消息

xsl:mumber插入一個格式化整數

xsl:processing-instruction創建一個處理指令

xsl:text計算xslt結果樹中的文本節點

xsl:value-of將單個源節點的字符串複製給輸出樹

xsl:variable可用做最高級元素或模板中的指令

'''''''''''''''''''''''''''''''''''''''

 

例子:

 

xsl生成超鏈接:

<a><xsl:attribute name="href">Name.asp</xsl:attribute><xsl:value-of select="name" /></a>

指令:

<xsl:processing-instruction name=”xml-stylesheet”>href=”employees.css”

  type=”text/css”</xsl:processing-instruction>

結果:<?xml-stylesheet href=”employees.css” type=”text/css” ?>

創建文字正文:

<xsl:text>some content would be nice</xsl:text>

 

 

應用xsl:for-each:

<xsl:for-each select=”*”>

  <xsl:choose>

    <xsl:when test=”name(parent::*)=’name’”>

       <xsl:value-of select=”.” />

       <xsl:text> </xsl:text>

    </xsl:when>

    <xsl:otherwise>

       <xsl:choose>

         <xsl:when test=”name()=’street’”>

            <xsl:value-of select=”.” />

            <br />

         </xsl:when>

         <xsl:when test=”name()=’city’”>

            <xsl:value-of select=”.” />

            <xsl:text>,</xsl:text>

         <xsl:when>

         <xsl:otherwise>

         <xsl:value-of select=”.” />

         <xsl:text>,</xsl:text>

         </xsl:otherwise>

       </xsl:choose>

    </xsl:otherwise>

   </xsl:choose>

 <xsl:for-each>

    

使用mode屬性促成多路處理:

<xsl:template match=”/”>

    <xsl:apply-templates select=”employees/employee” mode=”programming” />

</xsl:template>

 

<xsl:template match=”employee” mode=”programming”>

    <xsl:if test=”@dept=’programming’”>

    <xsl:value-of select=”contact/name” />

    </xsl:if>

</xsl:template>

 

<xsl:template match=”employee” mode=”training”>

    <xsl:if test=”@dept=’training’”>

    <xsl:value-of select=”contact/name” />

    </xsl:if>

</xsl:template>

發佈了24 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章