2017-08-15 58 views
0

我的主要XSLT文件導入多個其他XSLT文件,而這些XSLT文件還可以導入/包括0,1或多個XSLT文件(及以上級別也是可能的)應用所有的模板

我想一個從主xslt文件調用所有與特定模式相匹配的導入/包含模板(或者它們具有相同的名稱,相同的模式,相同的匹配或其他)。

我希望能夠做到這一點沒有編碼很難進口的特定列表(也就是說,如果你添加一個新的進口應該自動拾取)

另外,提取的價值具有給定名稱的變量。

在任一種情況下的結果應該被一起連接成一個單一的節點集。 結果的順序並不重要,包裝元素是可選的(但希望)

這可能嗎?

實施例輸入:

Main.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:import href="File1.xslt"/> 
    <!-- Some rules here, including the solution --> 
</xsl:style> 

File1.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:import href="File2.xslt"/> 
    <!-- Some unrelated rules here --> 
    <xsl:template name="Things"> 
    <!-- name could be mode or matches, or the template could be a variable instead --> 
    <Something/> 
    </xsl:template> 
</xsl:style> 

File2.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <!-- Some unrelated rules here --> 
    <xsl:template name="Things"> 
    <!-- name could be mode or matches, or the template could be a variable instead --> 
    <SomethingElse/> 
    </xsl:template> 
</xsl:style> 

需要的輸出:

<xml> 
    <Something/> 
    <SomethingElse/> 
</xml> 

注:在我的場景中,所需模板/變量的內容將是靜態的,儘管有一個解決方案可以包含xslt。

+1

告訴我們你想實現(什麼是你的輸入和期望的輸出)什麼,有可能實現它,這種說法並不如此怪異的方式。 –

+0

+ Michael Kay請參閱編輯 – DJL

+0

您已對您要編寫的代碼進行了改進。我想知道你真的想達到什麼。 –

回答

0

,我想出了一個稍微哈克的方式做到這一點。

<xsl:template name="EntryPoint"> 
    <xml> 
    <xsl:apply-templates select="document('')/xsl:stylesheet/xsl:import/@href" mode="FindThings"/> 
    </xml> 
</xsl:template> 
<xsl:template mode="FindThings" match="/xsl:stylesheet"> 
    <xsl:apply-templates select="document(xsl:import/@href)" mode="FindThings"/> 
    <xsl:apply-templates select="xsl:variable[@name='Things']" mode="FindThings"/> 
</xsl:template> 
<xsl:template mode="FindThings" match="xsl:variable[@name='Things']"> 
    <xsl:copy-of select="*"/> 
</xsl:template> 
<xsl:template mode="FindThings" match="xsl:import/@href"> 
    <xsl:apply-templates select="document(.)" mode="FindThings"/> 
</xsl:template> 

它輸出全稱爲「物聯網」每個導入XSLT文件中,進口的,無論深度頂級變量的內容。