我有下面的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或文檔類型,我嘗試複製爲好,但我不知道。
你可以顯示'reference.dtd'文件嗎? –
DTD可以定義屬性的默認值。 –