2016-05-23 66 views
0

我有下面的XML文件:XSLT - 複製模板增加了意想不到的屬性

<?xml version="1.0"?> 
<!DOCTYPE reference PUBLIC "-//OASIS//DTD DITA Reference//EN" "dtd/reference.dtd"> 
<!--<!DOCTYPE reference PUBLIC "-//OASIS//DTD DITA Reference//EN" "dtd/reference.dtd">--> 
<reference xml:lang="en-us" id="D609"> 
    <title>Body Text</title> 
    <shortdesc>A short desc.</shortdesc> 
    <prolog> 
     <metadata/> 
    </prolog> 
    <refbody> 
     <section/> 
    </refbody> 
</reference> 

我只是想在裏面添加一些元素。 所以,我只需運行一個這樣的複製模板:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">   


    <xsl:template match="/"> 

    <!-- Get the DOCTYPE comment --> 
    <xsl:variable name="d" 
     select="//comment()[contains(.,'DOCTYPE')][1]" /> 
    <xsl:variable name="doctype" select="substring($d,0)" /> 
    <xsl:message select="$doctype" /> 

    <!-- Output the DOCTYPE --> 
    <xsl:value-of disable-output-escaping="yes" select="$doctype" /> 

    <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="comment()[contains(text(),DOCTYPE)]"> 
    </xsl:template> 

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

而不是得到什麼,我認爲那些是完全相同的輸出中和,我得到的是:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE reference PUBLIC "-//OASIS//DTD DITA Reference//EN" "dtd/reference.dtd"> 
<reference xml:lang="en-us" id="D609" DTDVersion="V1.1.3" 
    domains="(topic ui-d) (topic hi-d) (topic pr-d) (topic sw-d) (topic ut-d)" 
    class="- topic/topic  reference/reference "> 
    <title class="- topic/title ">Body Text</title> 
    <shortdesc class="- topic/shortdesc ">A short desc.</shortdesc> 
    <prolog class="- topic/prolog "> 
     <metadata class="- topic/metadata "/> 
    </prolog> 
    <refbody class="- topic/body  reference/refbody "> 
     <section class="- topic/section "/> 
    </refbody> 
</reference> 

所以基本上,我得到一個每個元素的類屬性。我的參考標籤也用神奇地創建了一些標籤(對我來說)。

屬性從哪裏來?我如何擺脫它們?

我想它可能會涉及到DTD或文檔類型,我嘗試複製爲好,但我不知道。

+0

你可以顯示'reference.dtd'文件嗎? –

+1

DTD可以定義屬性的默認值。 –

回答

1

輸入到XSLT處理器來自XML解析器,並且該輸入的形式是(邏輯上)一個節點,其詳細形式由XDM數據模型中定義的樹。如果XML解析器是驗證解析器(由DTD中的元素和屬性定義驅動),那麼解析器傳遞給XSLT引擎的樹通常不僅包含源中明確存在的屬性,還包含用於哪些默認值已經在DTD中定義。 XDM模型不區分顯式和隱式屬性,因此XSLT以相同的方式對待兩者。

一些XSLT處理器(或XML解析器)可以有多種選擇來排除從輸入樹的XDM模型違約屬性。例如,使用Saxon時,可以在從命令行運行時使用-expand:off,或者在通過Java或.NET API運行時使用類似選項來實現。如果沒有這種選擇,那麼最好的辦法是避免驗證DTD的輸入;如何做到這一點的細節取決於你的XML解析器/ XSLT處理器組合。

+0

您好邁克爾,謝謝你的詳細解答,現在我明白了:)對於那些你螞蟻運行撒克遜使用此選項調用的是: '<工廠名稱=「net.sf.saxon.TransformerFactoryImpl」>'' ' '' – Flag