XML-XSLT避免重复然后组合

时间:2015-10-31 14:22:06

标签: xml xslt

当我已经对它进行了ID转换时,我很难停止XSLT转换。我已经知道一旦转换避免重复,如何组合缺失的部分。

任何帮助都会非常感谢,谢谢!

MY XML FILE

   ?xml version="1.0" encoding="UTF-8"?>
<row>
    <data>
        <id>1</id>
        <fname>Vcut</fname>
        <lname>Piatos</lname>
        <age>0</age>
        <code>1002</code>
        <hobby>dota</hobby>
    </data>
    <data>
        <id>1</id>
        <fname>Mayo</fname>
        <lname>Naise</lname>
        <age>22</age>
        <code>1003</code>
        <hobby>program</hobby>
    </data>
 <data>
        <id>2</id>
        <fname>Ben</fname>
        <lname>Ten</lname>
        <age>22</age>
        <code>1003</code>
        <hobby>Eat</hobby>
    </data>
</row>

XSLT TRANSLATION

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">

 <xsl:for-each select="row/data">
<EmployeeID>
<xsl:for-each-group select="id" group-by="id">
 <xsl:value-of select="current-grouping-key()"/>
</xsl:for-each-group>
    </EmployeeID>
         <EmployeeName>
         <xsl:value-of select='concat(fname," ",lname)' />
        </EmployeeName>
      <EmployeeAge>
            <xsl:value-of select='age'/>
    </EmployeeAge>
      <EmployeeCode><xsl:value-of select="code"/></EmployeeCode>
      <EmployeeActivity><xsl:value-of select="hobby"/></EmployeeActivity>
   ------------------------------------------------------------------------
</xsl:for-each>

</xsl:template>


</xsl:stylesheet>

输出

<?xml version="1.0" encoding="UTF-8"?>
<EmployeeID>1</EmployeeID>
<EmployeeName>Vcut Piatos</EmployeeName>
<EmployeeAge>0</EmployeeAge>
<EmployeeCode>1002</EmployeeCode>
<EmployeeActivity>dota</EmployeeActivity>
   ------------------------------------------------------------------------
<EmployeeID/>
<EmployeeName>Mayo Naise</EmployeeName>
<EmployeeAge>22</EmployeeAge>
<EmployeeCode>1003</EmployeeCode>
<EmployeeActivity>program</EmployeeActivity>
   ------------------------------------------------------------------------
<EmployeeID>2</EmployeeID>
<EmployeeName>Ben Ten</EmployeeName>
<EmployeeAge>22</EmployeeAge>
<EmployeeCode>1003</EmployeeCode>
<EmployeeActivity>Eat</EmployeeActivity>
   ------------------------------------------------------------------------

期望的输出

<?xml version="1.0" encoding="UTF-8"?>
<EmployeeID>1</EmployeeID>
<EmployeeName>Vcut Piatos</EmployeeName>
<EmployeeAge>0</EmployeeAge>
<EmployeeCode>1002</EmployeeCode>
<EmployeeActivity>dota</EmployeeActivity>
<EmployeeActivity>program</EmployeeActivity>
   ------------------------------------------------------------------------
<EmployeeID>2</EmployeeID>
<EmployeeName>Ben Ten</EmployeeName>
<EmployeeAge>22</EmployeeAge>
<EmployeeCode>1003</EmployeeCode>
<EmployeeActivity>Eat</EmployeeActivity>
   ------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:1)

您需要按dataid进行分组,id进行id分组 - 您需要在row的上下文中执行此操作,而不是data的背景:

XSLT 2.0

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:template match="/row">
    <xsl:for-each-group select="data" group-by="id">
        <EmployeeID>
            <xsl:value-of select="current-grouping-key()"/>
        </EmployeeID>
        <EmployeeName>
            <xsl:value-of select='concat(fname," ",lname)' />
        </EmployeeName>
        <EmployeeAge>
            <xsl:value-of select='age'/>
        </EmployeeAge>
        <EmployeeCode>
            <xsl:value-of select="code"/>
        </EmployeeCode>
        <EmployeeActivity>
            <xsl:value-of select="hobby"/>
        </EmployeeActivity>
------------------------------------------------------------------------
    </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

注意

  1. 结果不是格式良好的XML,因为它没有单个根元素;
  2. 文本分隔符可能对人类读者有帮助,但可能会成为进一步处理的障碍 - 请考虑使用注释;
  3. 获得的结果与您发布的结果不同:第一位员工的活动为data,而不是program