XSL 1.0转换以合并节点

时间:2010-06-21 21:34:34

标签: xslt

是否可以使用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>

<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>

1 个答案:

答案 0 :(得分: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>
相关问题