2009-08-20 62 views
0

喂, 首先,我不是很熟悉,當涉及到XML和similiar,所以請不要懲罰我與我的初學者的問題:d如何避免所有參數?

我有一個xml文件看起來像這樣:

<?xml version="1.0" encoding="utf-8" ?> 
<mainstuff> 
    <category_major> 
     <project_name>Dream</project_name> 
     <project_attribute>Version 1.0</project_attribute> 
     <category_A></category_A> 
     <category_B></category_B> 
     <category_C></category_C> 
    </category_major> 
</mainstuff> 

後來我有一個XSLT文件看起來像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="xml" indent="yes"/> 

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

    <xsl:template match="/"> 
    <xsl:element name="mainstuff"> 
     <xsl:attribute name="version">1.0</xsl:attribute> 
     <xsl:apply-templates/> 
    </xsl:element>  
    </xsl:template> 

    <xsl:template match="category_major"> 
    <xsl:element name="category_major"> 
     <xsl:apply-templates/> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="category_A"> 
    <xsl:element name="category_A"> 
     <xsl:apply-templates/> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="category_B"> 
    <xsl:element name="category_B"> 
     <xsl:apply-templates/> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="category_C"> 
    <xsl:element name="category_C"> 
     <xsl:apply-templates/> 
    </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 

我想避免的兩個參數「PROJECT_NAME」和「project_attribute」。我想這樣的結果:

<?xml version="1.0" encoding="utf-8" ?> 
<mainstuff> 
    <category_major> 
     <category_A></category_A> 
     <category_B></category_B> 
     <category_C></category_C> 
    </category_major> 
</mainstuff> 

但我得到改造的探討後是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<mainstuff version="1.0"> 
    <category_major> 
     **Dream 
     Version 1.0** 
     <category_A /> 
     <category_B /> 
     <category_C /> 
    </category_major> 
</mainstuff> 

文本仍處於它。我如何解決它。我究竟做錯了什麼 ?我怎麼能意識到獲取參數,但沒有其中的文字?在我的例子是這樣的輸出:

<?xml version="1.0" encoding="utf-8" ?> 
<mainstuff> 
    <category_major> 
     **<project_name></project_name> 
     <project_attribute></project_attribute>** 
     <category_A></category_A> 
     <category_B></category_B> 
     <category_C></category_C> 
    </category_major> 
</mainstuff> 

感謝您的幫助:d

+0

請澄清 - 這導致你要嗎?你詢問了沒有參數和它們的值的結果,最後你詢問了沒有它們的值的參數? – 2009-08-20 20:38:15

+0

我想要每個結果。一個接一個:D – Camal 2009-08-21 07:21:25

回答

2

如果沒有模板相匹配的元素,一個默認的模板被使用。默認模板的效果是有效地輸出節點的字符串值 - 對於一個元素,它看起來像是所有後代文本節點的串聯。

如果要覆蓋此行爲,您需要爲您希望跳過的元素提供自己無操作模板:

<xsl:template match="project_name | project_attribute" /> 

對於你的第二個要求,如果你想輸出的元件,但去除所有內容,可以使用xsl:copy

<xsl:template match="project_name | project_attribute"> 
    <xsl:copy /> 
</xsl:template> 

注意xsl:copy只複製的元素;它不會複製它的屬性,也不會複製它的子項。

+0

謝謝偉大的作品:D – Camal 2009-08-21 07:48:09

0

嘗試反其道而行,匹配您要排除什麼,而不是你想要的東西包括:

<xsl:template match="/"> 
    <xsl:apply-templates /> 
</xsl:template> 

<xsl:template match="*" priority="0"> 
    <xsl:element name="{local-name()}"> 
     <xsl:apply-templates /> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="project_name" priority="1"></xsl:template> 
<xsl:template match="project_attribute" priority="1"></xsl:template> 

很抱歉,如果這是一個有點神祕,但我希望它能幫助

+0

身份轉換(這是你似乎試圖用於'match =「*」')的最好使用'xsl:copy'編寫。而且,不需要明確的優先級 - 模板匹配的優先級總是低於與命名元素匹配的模板的優先級。 – 2009-08-20 20:45:47

+0

優先級是可讀性的優先級,我總是在要覆蓋的模板上包含priority =「0」,個人風格 和no,xsl:copy不會將模板應用於元素子元素,解決方案工作 – LorenVS 2009-08-20 20:47:54

+0

您可以在''內使用''(或任何其他構造函數)。 – 2009-08-21 17:08:14