共用方式為


<xsl:processing-instruction> 元素

產生輸出中的處理指示節點。

<xsl:processing-instruction
  name = "pi-name">
</xsl: processing-instruction>

屬性

  • name
    必要項。 處理指示的 NCName。

項目資訊

發生次數

無限制

父項目

xsl:attributexsl:commentxsl:copyxsl:elementxsl:fallbackxsl:for-eachxsl:ifxsl:messagexsl:otherwisexsl:param、xsl:processing-instruction、xsl:templatexsl:variablexsl:whenxsl:with-param、輸出項目

項目子系

xsl:apply-importsxsl:apply-templatesxsl:call-templatexsl:choosexsl:copyxsl:copy-ofxsl:fallbackxsl:for-eachxsl:ifxsl:messagexsl:numberxsl:textxsl:value-ofxsl:variable

備註

<xsl:processing-instruction> 項目產生輸出中的處理指示節點。 名稱由 name 屬性所指示。 此項目的內容可提供其他處理指示。

XML 宣告並非處理指示,而應透過設定 <xsl:output> 項目的屬性來產生。

範例

此範例顯示一個範本,它可在輸出中產生 XML 宣告與樣式表處理指示。

XML 檔 (customers.xml)

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="pi.xsl" ?>
<customers>
<customer>
   <name>James Smith</name>
   <address>123 Elm St.</address>
   <phone>(123) 456-7890</phone>
</customer>
<customer>
   <name>Amy Jones</name>
   <address>456 Oak Ave.</address>
   <phone>(156) 789-0123</phone>
</customer>
</customers>

XSLT 檔 (pi.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:output method='xml' version='1.0'/>
<xsl:template match="/">
  <xsl:processing-instruction name="xml-stylesheet">
  <xsl:text>type="text/xsl" href="style.xsl"</xsl:text>
  </xsl:processing-instruction>
  <xsl:apply-templates />
</xsl:template>

  <xsl:template match="@* | *">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template></xsl:stylesheet>

請嘗試

  1. 複製上述程式碼並將其儲存在本機磁碟機的適當檔案中。

  2. 從命令提示字元中使用 msxsl.exe 公用程式 (請至 www.microsoft.com/downloads/ 下載) 執行此範例,如下所示:

    msxsl customers.xml pi.xsl -o new-cust.xml

輸出

此轉換的結果將會是相同的 XML 檔案,但其中已嵌入新的樣式表。 輸出檔 new-cust.xml 應如下所示:

<?xml version="1.0" encoding="UTF-16"?>

<?xml-stylesheet type="text/xsl" href="style.xsl" ?>

<customers>

<customer>

<name>James Smith</name>

<address>123 Elm St.</address>

<phone>(123) 456-7890</phone>

</customer>

<customer>

<name>Amy Jones</name>

<address>456 Oak Ave.</address>

<phone>(156) 789-0123</phone>

</customer>

</customers>