2012-05-17 145 views
-2

關於Martin的回答有一個問題: Martin Honnen的答案效果很好,但與根本因素無關。比方說,我有 「汽車」 作爲根元素:用XSL轉換XSL

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="foo"> 
     <cars> 
      <car1 /> 
      <car2 /> 
     </cars> 
    </xsl:template> 
</xsl:stylesheet> 

我想獲得:

<xsl:template match="foo"> 
    <cars> 
     <car1 /> 
     <car2 /> 
     <TANK /> 
    </cars> 
</xsl:template> 

對於這一點,我會用:

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

,它輸出確切的輸入,不需要改變我可以試試:

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

但它將會把TANK節點樣式表外,這樣的:

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

<xsl:template match="order"> 
    <cars> 
     <car1/> 
     <car2/> 
    </cars> 
</xsl:template> 
</xsl:stylesheet><TANK/> 

如何進去車TANK元素?

原題: 我有,我用它來轉換XML一個XSL:

XML_format1 -> XSL1 -> XML_format2

我需要改變這個第一個XSL文件(使用第二XSL)獲得第三XSL文件,它將輸出第三種格式的XML。總之:

XSL1 -> XSL2 -> XSL3 

XML_format1 -> XSL3 -> XML_format3 

使用下面的樣式表,我能夠複製的第一個XSL的內容,並且還跳過某些節點:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 

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

<xsl:template match="//skipThisNode"/> 

</xsl:stylesheet> 

我的問題:除了這個,我也需要改變一些節點的結構(補充一下),從這樣的:

<car> 
    <color>green</color> 
    <fuel>petrol</fuel> 
</car> 

要這樣:

<car> 
    <color>green</color> 
    <fuel>petrol</fuel> 
    <topSpeed>99</topSpeed> 
</car> 

LE:我可以創建一個模板來匹配,我需要給孩子補充到,像這樣的特定節點:

<xsl:template match="car"> 
    <color> 
     <!-- existing value-of --> 
    </color> 
    <fuel> 
     <!-- existing value-of --> 
    </fuel> 
    <topSpeed> 
     <!-- * new value-of * --> 
    </topSpeed> 
</xsl:template> 

但這似乎打算在上面。有一種更簡單的方法來實現我想要的嗎?

+4

爲什麼會自動生成新的XSLT樣式表?手工操作要簡單得多。 –

+1

不是推薦的方法。 –

回答

4

我寧願用

<xsl:param name="newSpeed" select="99"/> 

<xsl:template match="car"> 
    <xsl:copy> 
    <xsl:apply-templates/> 
    <topSpeed> 
     <xsl:value-of select="$newSpeed"/> 
    </topSpeed> 
    </xsl:copy> 
</xsl:template> 

,保持加工鏈起來,允許您添加模板轉換或在需要時刪除子和car元素的後裔。

我不確定我是否理解你的最新需求,因爲輸入看起來是一個完整的樣式表,但隨後作爲所需輸出,你只顯示了一個模板。因此,假如你有輸入作爲

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="order"> 
     <cars> 
      <car1 /> 
      <car2 /> 
     </cars> 
    </xsl:template> 
</xsl:stylesheet> 

,並要同時改造xsl:template的匹配屬性,以及在模板體內的文字結果元素,我會用

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

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

    <xsl:template match="xsl:template[@match = 'order']/@match"> 
    <xsl:attribute name="{name()}"> 
     <xsl:text>foo</xsl:text> 
    </xsl:attribute> 
    </xsl:template> 

    <xsl:template match="xsl:template[@match = 'order']/cars"> 
    <xsl:copy> 
     <xsl:apply-templates/> 
     <TANK/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

這樣,我得到(用撒克遜6.5.5測試)結果

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="foo"> 
     <cars> 
      <car1/> 
      <car2/> 
     <TANK/></cars> 
    </xsl:template> 
</xsl:stylesheet> 

這是希望你想要什麼(也許缺乏適當的縮進)。

+0

顯然,它不適用於根節點。我會編輯我原來的問題。 – Buffalo

+0

謝謝!它工作得很好(除了我在本地的命名空間問題)。 – Buffalo

+0

#Martin https://stackoverflow.com/questions/43589532/how-can-i-add-header-and-footer-xslt-files-to-another-xslt-file-to-convert-it-in – unknownbits