2010-06-21 94 views
1

是否可以使用XSL 1.0进行转换?如果可以,请发布一些示例代码,这可以让我开始朝正确的方向发展。XSL 1.0转换到合并节点

<Region> 
<RecType1><Amt> 100 </Amt></RecType1><RecType2><Name>XXX</Name></RecType2><RecType1><Amt> 200 </Amt></RecType1><RecType2><Name>YYY</Name></RecType2><RecType1><Amt> 300 </Amt></RecType1><RecType2><Name>ZZZ</Name></RecType2></Region> 

TO

<Region> 
<Payment><Amt>100</Amt><Name>XXX</Name></Payment><Payment><Amt>200</Amt><Name>YYY</Name></Payment><Payment><Amt>300</Amt><Name>ZZZ</Name></Payment></Region> 
+0

好问题(1)。查看我的答案获得完整的解决方案。 – 2010-06-21 22:34:56

回答

0

该转化

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

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

<xsl:template match="RecType1"> 
    <Payment> 
    <xsl:apply-templates select="* | following-sibling::RecType2[1]/*"/> 
    </Payment> 
</xsl:template> 

<xsl:template match="RecType2"/> 
</xsl:stylesheet> 

当所提供的XML文档施加(缩进是可读的):

<Region> 
    <RecType1> 
     <Amt> 100 </Amt> 
    </RecType1> 
    <RecType2> 
     <Name>XXX</Name> 
    </RecType2> 
    <RecType1> 
     <Amt> 200 </Amt> 
    </RecType1> 
    <RecType2> 
     <Name>YYY</Name> 
    </RecType2> 
    <RecType1> 
     <Amt> 300 </Amt> 
    </RecType1> 
    <RecType2> 
     <Name>ZZZ</Name> 
    </RecType2> 
</Region> 

产生所需的结果(也缩进是可读的):

<Region> 
    <Payment> 
     <Amt> 100 </Amt> 
     <Name>XXX</Name> 
    </Payment> 
    <Payment> 
     <Amt> 200 </Amt> 
     <Name>YYY</Name> 
    </Payment> 
    <Payment> 
     <Amt> 300 </Amt> 
     <Name>ZZZ</Name> 
    </Payment> 
</Region>