2016-11-23 62 views
0

這是XML我集團通過XSLT或前sibbling

<?xml version="1.0" encoding="UTF-8"?> 
<Message> 
    <CarouselMap> 
     <Panel>FLWPB</Panel> 
     <Panel>FLWPB</Panel> 
     <Panel>FLWBM</Panel> 
     <Panel>ALPS</Panel> 
     <Panel>MM</Panel> 
     <Panel>ACUTE</Panel> 
     <Panel>CD10B</Panel> 
     <Panel>PNH</Panel> 
     <Panel>FLWBM</Panel> 
    </CarouselMap> 
</Message> 

我需要使用

<xsl:for-each-group select="Message/CarouselMap" group-by="Panel"> 

我需要把FLWPB,並FLWBM在同一組,並在單獨的休息組。這樣

 <Panel>FLWPB</Panel> 
     <Panel>ALPS</Panel> 
     <Panel>MM</Panel> 
     <Panel>ACUTE</Panel> 
     <Panel>CD10B</Panel> 
     <Panel>PNH</Panel> 

東西,我可以一個後綴添加到每個面板的端部像

<Message> 
    <CarouselMap> 
     <Panel>FLWPB_AA</Panel> 
     <Panel>FLWPB_AA</Panel> 
     <Panel>FLWBM_AA</Panel> 
     <Panel>ALPS_BB</Panel> 
     <Panel>MM_CC</Panel> 
     <Panel>ACUTE_DD</Panel> 
     <Panel>CD10B_EE</Panel> 
     <Panel>PNH_FF</Panel> 
     <Panel>FLWBM_AA</Panel> 
    </CarouselMap> 
</Message> 

然後用

<xsl:for-each-group select="Message/CarouselMap" group-by="substring_after(Panel, '_')"> 

但我認爲有可能是一個更好的辦法。你能否提供一個提示或更好的解決方案? 在此先感謝。

回答

1

你的問題表明你能夠操縱源數據。既然如此,我想補充一個 「分組鍵」,每一個元素這樣的:

<Message> 
    <CarouselMap> 
     <Panel key="FLW">FLWPB</Panel> 
     <Panel key="FLW">FLWPB</Panel> 
     <Panel key="FLW">FLWBM</Panel> 
     <Panel key="ALPS">ALPS</Panel> 
     <Panel key="MM">MM</Panel> 
     <Panel key="ACUTE">ACUTE</Panel> 
     <Panel key="CD10B">CD10B</Panel> 
     <Panel key="PNH">PNH</Panel> 
     <Panel key="FLW">FLWBM</Panel> 
    </CarouselMap> 

現在,使用下面的XSLT

<xsl:output method="xml"/> 
    <xsl:template match="/Message/CarouselMap"> 

    <xsl:for-each-group select="Panel" group-by="@key"> 
<Panel><xsl:value-of select="."/></Panel> 
    </xsl:for-each-group> 

    </xsl:template> 

給你你要的XML:

<Panel>FLWPB</Panel> 
<Panel>ALPS</Panel> 
<Panel>MM</Panel> 
<Panel>ACUTE</Panel> 
<Panel>CD10B</Panel> 
<Panel>PNH</Panel> 
0

我需要把FLWPB和FLWBM放在同一個組中p和其餘的在 分開組。

我假設你真的想放FLWPBFLWBM在同一組,並在不同的休息 - 一組爲每個不同的值。

這可能這樣做可以實現:

<xsl:template match="/Message"> 
    <root> 
     <xsl:for-each-group select="CarouselMap/Panel" group-by="if (. = 'FLWBM') then 'FLWPB' else ."> 
      <group> 
       <xsl:apply-templates select="current-group()"/> 
      </group> 
     </xsl:for-each-group> 
    </root> 
</xsl:template> 

無需修改原來的輸入。