2011-10-29 40 views
4

下面是我的XML文件 - 下面單個XSLT文件能解決這個問題嗎?或者?

<CVs> 
    <CV> 
    <Name>ABC</Name> 
    <Address></Address> 
    <Introduction></Introduction> 
    <CompSkills>Java, XSLT, XPATH, XML, Oracle, VB.NET</CompSkills> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    <CV> 
    <CV> 
    <Name>XYZ</Name> 
    <Address></Address> 
    <Introduction></Introduction> 
    <CompSkills>Java, XSLT, XPATH, XML, JSP, HTML</CompSkills> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    <CV> 
</CVs> 

是XSLT文件 - 簡短版本,以獲得一個想法

<xsl:template match="Name"> 
<table align='center' width='800' style="font-family:tahoma; font-size:13pt;"> 
<tr><td> 
    <xsl:apply-templates/> 
    </td></tr> 
    </table> 
</xsl:template> 

<xsl:template match="Experience"> 
<table align='center' width='800' style="font-family:tahoma; font-size:13pt;"> 
<tr><td> 
    <xsl:apply-templates/> 
    </td></tr> 
    </table> 
</xsl:template> 

我使用Java作爲前端。要以HTML格式顯示輸出,我有一個XSLT文件。這個XSLT文件是一個標準的文件,即;它顯示所有的簡歷。

現在我要做的就是用一個列表框與所有候選人的名字和特定名稱點擊時,只有他的簡歷應該得到顯示。我編寫了Java部分,將候選人的名字顯示在列表框中。現在,在以HTML格式顯示所選候選人的簡歷時遇到了一些麻煩。

當前的XSLT文件顯示所有CV。所以我需要另一個XSLT文件,它使用從程序傳遞的參數並顯示其細節。如果是的話,那麼有關如何做到這一點的幫助......?

預先感謝約翰 -

+0

好問題,+1。您需要將所選名稱作爲全局參數傳遞給變換 - 然後執行想要的變換很容易。 –

+0

Dimitre-非常感謝您的回答。你的意思是我必須首先將所需匹配提取到XML輸出中,然後將此文件傳遞給XSLT文件以獲取HTML輸出。再次感謝您的答覆。 :) – John

+0

@_John:不,我的意思是說在調用轉換的代碼中,您必須設置一個帶有想要的CV名稱(或*)的參數。我不知道你使用的是什麼特定的Java XSLT處理器。我通常使用.NET的XslCompiledTransform,這裏描述瞭如何爲轉換設置一個外部參數:http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltargumentlist.addparam.aspx For撒克遜人,看看薩克森文檔中的這些示例應用程序:http://www.saxonica.com/documentation9.2/samples/intro.html –

回答

2

爲了給你一個想法如何可以做到這一點,這裏是一個完整的解決方案提取全部或只是想CV元素(非HTML格式完成,因爲這是不相關的問題):

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="pName" select="'XYZ'"/> 

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

<xsl:template match="CV"> 
    <xsl:if test="$pName = Name or $pName='*'"> 
    <xsl:call-template name="identity"/> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

當該變換被應用到所提供的XML文檔(校正爲良好的形成的一個):

<CVs> 
    <CV> 
    <Name>ABC</Name> 
    <Address></Address> 
    <Introduction></Introduction> 
    <CompSkills>Java, XSLT, XPATH, XML, Oracle, VB.NET</CompSkills> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    </CV> 
    <CV> 
    <Name>XYZ</Name> 
    <Address></Address> 
    <Introduction></Introduction> 
    <CompSkills>Java, XSLT, XPATH, XML, JSP, HTML</CompSkills> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    <Experience> 
     <Profile></Profile> 
     <Duration></Duration> 
     <Info></Info> 
    </Experience> 
    </CV> 
</CVs> 

的希望,正確的(只是CVName XYZ被提取)產生

<CVs> 
    <CV> 
     <Name>XYZ</Name> 
     <Address/> 
     <Introduction/> 
     <CompSkills>Java, XSLT, XPATH, XML, JSP, HTML</CompSkills> 
     <Experience> 
     <Profile/> 
     <Duration/> 
     <Info/> 
     </Experience> 
     <Experience> 
     <Profile/> 
     <Duration/> 
     <Info/> 
     </Experience> 
     <Experience> 
     <Profile/> 
     <Duration/> 
     <Info/> 
     </Experience> 
    </CV> 
</CVs> 

說明

想要的名稱或 「*」 必須在外部作爲一個全球性的參數傳遞(在這種情況下命名爲pName)進行轉換。請閱讀您的XSLT處理器文檔,瞭解如何完成此操作,因爲這取決於實現。

3

你可以做的是增加一個xsl:param到您的XSLT,並給它一個默認值;例如,「全部」。這種方式默認情況下會顯示全部CV s。

如果你需要顯示基於單一CVName,你可以在你xsl:param傳遞值(從你的列表框),僅顯示CV

下面是一個例子xsl:param,並顯示CV小號所需的xsl:template:如果你想只顯示XYZ CV

<xsl:param name="pName" select="'All'"/> 

    <xsl:template match="CV"> 
    <xsl:if test="$pName = 'All' or Name = $pName"> 
     <xsl:apply-templates/> 
    </xsl:if> 
    </xsl:template> 

,你只需使用值XYZpName PARAM當你調用XSLT。

+0

@ DevNull-感謝您的答案。我會嘗試它,並再次回來,無論是感謝或問題... :) – John

+0

@約翰 - 非常歡迎,但也許你寧願等待接受我的答案?我欣賞它,但。 :-) –

+0

@ DevNull-好吧,我已經取消了接受..成功後給出的代碼,我一定會接受它..請不要介意。 – John