2016-06-13 42 views
0

我有下面的XML:XSL - 组和CONCAT值

<Records> 
     <Record> 
      <ReportId>1111</ReportId> 
      <ReportEntryId>2</ReportEntryId> 
      <TaxAuthorityLabel>AA</TaxAuthorityLabel> 
      <FutureUse /> 
      <FutureUse32 /> 
     </Record> 

     <Record> 
      <ReportId>1111</ReportId> 
      <ReportEntryId>2</ReportEntryId> 
      <TaxAuthorityLabel>ZZ</TaxAuthorityLabel> 
      <FutureUse /> 
      <FutureUse32 /> 
     </Record> 

     <Record> 
      <ReportId>1111</ReportId> 
      <ReportEntryId>3</ReportEntryId> 
      <TaxAuthorityLabel>XX</TaxAuthorityLabel> 
      <FutureUse /> 
      <FutureUse32 /> 
     </Record> 

     <Record> 
      <ReportId>1111</ReportId> 
      <ReportEntryId>3</ReportEntryId> 
      <TaxAuthorityLabel>EE</TaxAuthorityLabel> 
      <FutureUse /> 
      <FutureUse32 /> 
     </Record> 
    </Records> 

我需要组/ CONCAT FutureUse下的所有TaxAuthorityLabel值每一个独特的ReportEntryId值。有什么需要此转换的是:

<Records> 
     <ReportId>1111</ReportId> 
     <ReportEntryId>2</ReportEntryId> 
     <TaxAuthorityLabel>AA</TaxAuthorityLabel> 
     <FutureUse>AA_ZZ</FutureUse> 
     <FutureUse32 /> 
    </Records> 

    <Records> 
     <ReportId>1111</ReportId> 
     <ReportEntryId>2</ReportEntryId> 
     <TaxAuthorityLabel>ZZ</TaxAuthorityLabel> 
     <FutureUse>AA_ZZ</FutureUse> 
     <FutureUse32 /> 
    </Records> 

    <Records> 
     <ReportId>1111</ReportId> 
     <ReportEntryId>3</ReportEntryId> 
     <TaxAuthorityLabel>XX</TaxAuthorityLabel> 
     <FutureUse>XX_EE</FutureUse> 
     <FutureUse32 /> 
    </Records> 

    <Records> 
     <ReportId>1111</ReportId> 
     <ReportEntryId>3</ReportEntryId> 
     <TaxAuthorityLabel>EE</TaxAuthorityLabel> 
     <FutureUse>XX_EE</FutureUse> 
     <FutureUse32 /> 
    </Records> 

,我有所有节点下concats所有TaxAuthorityLabel值的XSL。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:mapping="mapping:mapping" exclude-result-prefixes="mapping"> 
    <xsl:output method="xml" 
       encoding="UTF-8" 
       omit-xml-declaration="no" 
       indent="yes" /> 

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

    <xsl:template match="FutureUse"> <!-- we are setting every FutureUse --> 
     <xsl:for-each select="../ReportEntryId" > 
      <FutureUse> 
       <xsl:if test=". = 2" > 
        <xsl:for-each select="../../Records/TaxAuthorityLabel" > 
         <xsl:value-of select="." /> 
         <xsl:value-of select="'_'" /> 
        </xsl:for-each> 
       </xsl:if> 
      </FutureUse> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

我发现它难以获得ReportEntryId的前一个值,所以我可以对它们进行比较。

任何帮助,这是非常感谢。

回答

1

我建议你试试这样说:

XSLT 1.0

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

<xsl:key name="record-by-entry" match="Record" use="ReportEntryId" /> 

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

<xsl:template match="FutureUse"> 
    <xsl:copy> 
     <xsl:for-each select="key('record-by-entry', ../ReportEntryId)" > 
      <xsl:value-of select="TaxAuthorityLabel" /> 
      <xsl:if test="position()!=last()"> 
       <xsl:text>_</xsl:text> 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet>