如何使用重复元素组合多行中的元素

时间:2016-05-16 18:24:02

标签: xslt

下面是我的输入xml,我想使用xslt 1.0转换为下面指定的xml。

元素ref需要用作键合并在输出中创建数组 输入xml:

<Input>
     <row>
        <Name>ABC</Name>
        <Ref>12345</Ref>
        <Status>O</Status>
         <Currency>USD</Currency>
        <Date>2016-05-16</Date>
    </row>
     <row>
        <Name>ABC</Name>
        <Ref>12345</Ref>
         <Status>O</Status>
        <Currency>AUD</Currency>
        <Date>2016-05-01</Date>
    </row>
    <row>
        <Name>XYZ</Name>
         <Ref>54321</Ref>
        <Status>O</Status>
        <Currency>AUD</Currency>
         <Date>2016-03-01</Date>
    </row>
    <row>
        <Name>XYZ</Name>
         <Ref>54321</Ref>
         <Status>O</Status>
         <Currency>USD</Currency>
        <Date>2016-05-01</Date>
    </row>
</Input>

输出Xml:使用请求中的ref元素,输出xml中只有两个行元素,但不同的值在另一个子节点中进行了分组。

<Output>
    <row>
        <Name>ABC</Name>
         <Ref>12345</Ref>
        <Status>O</Status>
         <Details>
                 <Detail>
                    <Currency>USD</Currency>
                     <Date>2016-05-16</Date>
                </Detail>
                 <Detail>
                    <Currency>AUD</Currency>
                    <Date>2016-05-01</Date>
                 </Detail>
            </Details>
     </row>
     <row>
        <Name>XYZ</Name>
        <Ref>54321</Ref>
         <Status>O</Status>
         <Details>
                 <Detail>
                     <Currency>AUD</Currency>
                      <Date>2016-03-01</Date>
                 </Detail>
                 <Detail>
                     <Currency>USD</Currency>
                    <Date>2016-05-01</Date>
                </Detail>
         </Details>
     </row>
</Output>

任何帮助?

尝试使用以下xslt,但if条件不起作用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output method="xml" version="1.0"/>
<xsl:template match="/">
<xsl:element name="Output">
    <xsl:apply-templates select="Input"/>
</xsl:element>  
</xsl:template>
<xsl:template match="Input">
<xsl:for-each select="row">
    <xsl:if test="(following-sibling::Ref = /Ref)">
      <xsl:element name="row">
           <xsl:element name="Name">
                <xsl:value-of select="Name" />
            </xsl:element>
            <xsl:element name="Ref">
                <xsl:value-of select="Ref" />
            </xsl:element>
            <xsl:element name="Status">
                <xsl:value-of select="Status" />
            </xsl:element>
            <xsl:element name="Details"> 
               <xsl:element name="Detail"> 
                <xsl:element name="Currency">
                    <xsl:value-of select="Currency" />
                </xsl:element>
                <xsl:element name="Date">
                    <xsl:value-of select="Date" />
                </xsl:element>
               </xsl:element>
            </xsl:element>
        </xsl:element>
        </xsl:if>   
    </xsl:for-each> 
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

此转化

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kRowByRefNameStat" match="row" use="concat(Name, '|', Ref, '|', Status)"/>

  <xsl:template match="/*">
    <Output>
      <xsl:apply-templates select=
      "row[generate-id()
      =
       generate-id(key('kRowByRefNameStat', 
                        concat(Name, '|', Ref, '|', Status))[1])]"/>
    </Output>
  </xsl:template>
  <xsl:template match="row">
    <row>
      <xsl:copy-of select="Name | Ref | Status"/>
      <Details>
          <xsl:apply-templates mode="inGroup" select=
          "key('kRowByRefNameStat', concat(Name, '|', Ref, '|', Status))"/>
        </Details>
    </row>
  </xsl:template>
  <xsl:template match="row" mode="inGroup">
    <Detail><xsl:copy-of select="Currency | Date"/></Detail>
  </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<Input>
     <row>
        <Name>ABC</Name>
        <Ref>12345</Ref>
        <Status>O</Status>
         <Currency>USD</Currency>
        <Date>2016-05-16</Date>
    </row>
     <row>
        <Name>ABC</Name>
        <Ref>12345</Ref>
         <Status>O</Status>
        <Currency>AUD</Currency>
        <Date>2016-05-01</Date>
    </row>
    <row>
        <Name>XYZ</Name>
         <Ref>54321</Ref>
        <Status>O</Status>
        <Currency>AUD</Currency>
         <Date>2016-03-01</Date>
    </row>
    <row>
        <Name>XYZ</Name>
         <Ref>54321</Ref>
         <Status>O</Status>
         <Currency>USD</Currency>
        <Date>2016-05-01</Date>
    </row>
</Input>

生成想要的正确结果

<Output>
   <row>
      <Name>ABC</Name>
      <Ref>12345</Ref>
      <Status>O</Status>
      <Details>
         <Detail>
            <Currency>USD</Currency>
            <Date>2016-05-16</Date>
         </Detail>
         <Detail>
            <Currency>AUD</Currency>
            <Date>2016-05-01</Date>
         </Detail>
      </Details>
   </row>
   <row>
      <Name>XYZ</Name>
      <Ref>54321</Ref>
      <Status>O</Status>
      <Details>
         <Detail>
            <Currency>AUD</Currency>
            <Date>2016-03-01</Date>
         </Detail>
         <Detail>
            <Currency>USD</Currency>
            <Date>2016-05-01</Date>
         </Detail>
      </Details>
   </row>
</Output>

<强>解释

  1. Muenchian grouping 使用复合键

  2. <强> Modes