使用XSLT 1.0对匹配行的其余节点进行分组和移动

时间:2017-06-29 10:25:59

标签: xslt xslt-1.0

我有以下输入xml:

    <?xml version="1.0" encoding="UTF-8"?>
<response>
   <case>
      <CMEDIA>Phone</CMEDIA>
   </case>
   <results>
      <row>
         <IKEY>TestKey1</IKEY>
         <OBJECTID>TestObject1</OBJECTID>
         <OBJECTPHYFILEID>ObjPhyField1</OBJECTPHYFILEID>
      </row>
      <row>
         <IKEY>TestKey1</IKEY>
         <OBJECTID>TestObject2</OBJECTID>
         <OBJECTPHYFILEID>ObjPhyField2</OBJECTPHYFILEID>
      </row>
      <row>
         <IKEY>TestKey1</IKEY>
         <OBJECTID>TestObject3</OBJECTID>
         <OBJECTPHYFILEID>ObjPhyField3</OBJECTPHYFILEID>
      </row>
      <row>
         <IKEY>TestKey4</IKEY>
         <OBJECTID>TestObject4</OBJECTID>
         <OBJECTPHYFILEID>ObjPhyField4</OBJECTPHYFILEID>
      </row>
   </results>
</response>

我的要求是将所有匹配的<IKEY>行&amp;将它们移到一个<row>下并移动<OBJECTID>&amp; <OBJECTPHYFILEID>节点存在于新节点<IKEY>下的匹配<SourceInstance>行(即同一行)中。最终我的输出xml应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <case>
      <CMEDIA>Phone</CMEDIA>
   </case>
   <results>
      <row>
         <IKEY>TestKey1</IKEY>
         <Sources>
            <SourceInstance>
               <OBJECTID>TestObject1</OBJECTID>
               <OBJECTPHYFILEID>ObjPhyField1</OBJECTPHYFILEID>
            </SourceInstance>
            <SourceInstance>
               <OBJECTID>TestObject2</OBJECTID>
               <OBJECTPHYFILEID>ObjPhyField2</OBJECTPHYFILEID>
            </SourceInstance>
            <SourceInstance>
               <OBJECTID>TestObject3</OBJECTID>
               <OBJECTPHYFILEID>ObjPhyField3</OBJECTPHYFILEID>
            </SourceInstance>
         </Sources>
      </row>
      <row>
         <IKEY>TestKey4</IKEY>
         <Sources>
            <SourceInstance>
               <OBJECTID>TestObject4</OBJECTID>
               <OBJECTPHYFILEID>ObjPhyField4</OBJECTPHYFILEID>
            </SourceInstance>
         </Sources>
      </row>
   </results>
</response>

我正在尝试使用以下XSLT,但无法实现所需的xml:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" indent="yes" />
   <xsl:key name="ikey" match="row" use="string(IKEY)" />
   <xsl:template match="results">
      <xsl:copy>
         <xsl:apply-templates select="row[generate-id() = generate-id(key('ikey', string(IKEY))[1])]" mode="ikey" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="row" mode="ikey">
      <xsl:choose>
         <xsl:when test="IKEY">
            <row>
               <IKEY>
                  <xsl:value-of select="//IKEY" />
               </IKEY>
               <Sources>
                  <xsl:for-each select="OBJECTID">
                     <SourceInstance>
                        <xsl:value-of select="//key('ikey', IKEY)/OBJECTID" />
                     </SourceInstance>
                  </xsl:for-each>
               </Sources>
            </row>
         </xsl:when>
      </xsl:choose>
   </xsl:template>
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

我确信我在这里遗漏了一些东西。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

使用<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes" /> <xsl:key name="ikey" match="row" use="string(IKEY)" /> <xsl:template match="results"> <xsl:copy> <xsl:apply-templates select="row[generate-id() = generate-id(key('ikey', string(IKEY))[1])]" mode="ikey" /> </xsl:copy> </xsl:template> <xsl:template match="row" mode="ikey"> <xsl:choose> <xsl:when test="IKEY"> <row> <xsl:copy-of select="IKEY"/> <Sources> <xsl:apply-templates select="key('ikey', IKEY)"/> </Sources> </row> </xsl:when> </xsl:choose> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <xsl:template match="row"> <SourceInstance> <xsl:apply-templates select="*[not(self::IKEY)]"/> </SourceInstance> </xsl:template> </xsl:stylesheet> 功能,您可以找到当前组中的所有项目,然后根据需要对其进行转换:

{{1}}