2016-02-29 71 views
0

同一水平裹子元素我有一個包含一些其他的元素,像這樣的多次入境元素:基於未來元素的值在XSLT

<ENTRY> 
    <HEAD>samplehead</HEAD> 
    <NUM>1</NUM> 
    <EXPL></EXPL> 
    <TRAN></TRAN> 

    <NUM>2 </NUM> 
    <EXPL></EXPL> 
    <COMP></COMP> 

    <NUM>3 </NUM> 
    <TRAN></TRAN> 
    <DIS></DIS> 
</ENTRY> 

我想基礎上的num元素添加一個新的身體所以我會結束與

<element> 
    <body> 
    <expl></expl> 
    <tran></tran> 
    </body> 

    <body> 
    <expl></expl> 
    <comp>/comp> 
    </body> 

    <body> 
    <tran></tran> 
    <dis></dis> 
    </body> 
</element> 

如何實現這與xslt 1.0?

在此先感謝:)

回答

0

可以與身份模板

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

然後一個覆蓋模板開始

<!-- override the ENTRY node --> 
<xsl:template match="ENTRY"> 
    <element> 
     <!-- loop for each NUM node --> 
     <xsl:for-each select="NUM"> 
      <!-- store the current generate-id() --> 
      <xsl:variable name="ID" select="generate-id(.)"/> 
      <body> 
       <!-- apply following nodes, excluding the NUM node --> 
       <xsl:apply-templates select="following-sibling::*[not(self::NUM)][generate-id(preceding-sibling::NUM[1]) = $ID]"/> 
      </body> 
     </xsl:for-each> 
    </element> 
</xsl:template> 

因此整個樣式表:

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

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 

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

    <xsl:template match="ENTRY"> 
     <element> 
      <xsl:for-each select="NUM"> 
       <xsl:variable name="ID" select="generate-id(.)"/> 
       <body> 
        <xsl:apply-templates select="following-sibling::*[not(self::NUM)][generate-id(preceding-sibling::NUM[1]) = $ID]"/> 
       </body> 
      </xsl:for-each> 
     </element> 
    </xsl:template> 
</xsl:stylesheet> 
+0

非常感謝你:)我知道stackoverflow將交付! –