2016-11-25 33 views
0

我在XSLT搞清楚這個分組的一個問題:分組在XSLT 2.0(通過短信分組)

的初始信息:

<Application> 

    <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8a].UniqueID" Value="bfd0b74d-2888-49d9-a986-df807f08ad8a" /> 
    <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8a].Filename" Value="Document 1 Test" /> 
    <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8a].URI" Value="https/.test.pdf" /> 

    <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8b].UniqueID" Value="bfd0b74d-2888-49d9-a986-df807f08ad8b" /> 
    <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8b].Filename" Value="Document 2 Test" /> 
    <ApplicationItem LayoutPath="Attachments.Package.Attachment[bfd0b74d-2888-49d9-a986-df807f08ad8b].URI" Value="google.com" /> 

</Application> 

預期的結果:

<Package> 
    <Attachment UniqueID="bfd0b74d-2888-49d9-a986-df807f08ad8a" 
     Filename="Document 1 Test" 
     URI="https/.test.pdf"/> 
    <Attachment UniqueID="bfd0b74d-2888-49d9-a986-df807f08ad8b" 
     Filename="Document 2 Test" 
     URI="google.com"/> 
<Package> 

我代碼: 我已經通過使用方括號中的ID完成了分組。

<xsl:for-each-group select="ApplicationItem[contains(@LayoutPath,'Attachments.Package.Attachment')]" group-by="substring-before(substring-after(@LayoutPath, 'Attachments.Package.Attachment['), ']')"> 
      <Attachment> 
       <xsl:for-each select="current-group()"> 
        <xsl:attribute name="UniqueID" select="current-grouping-key()"/> 
        <xsl:attribute name="Filename" select=".[contains(@LayoutPath,'Filename')]/@Value"/> 
        <xsl:attribute name="URI" select=".[contains(@LayoutPath,'URI')]/@Value"/> 
       </xsl:for-each> 
      <Attachment> 
</xsl:for-each-group> 

我的結果:

<Package> 
    <Attachment UniqueID="bfd0b74d-2888-49d9-a986-df807f08ad8a" 
     Filename="" 
     URI="https/.test.pdf"/> 
    <Attachment UniqueID="bfd0b74d-2888-49d9-a986-df807f08ad8b" 
     Filename="" 
     URI="google.com"/> 
<Package> 

我需要在代碼更改爲使用分組,因爲現在不工作只服用具有獨特的@LayoutPath最後ApplicationItem什麼。 我認爲問題與分組有關,但現在不要如何解決它。

回答

1

取出<xsl:for-each select="current-group()">和改變

   <xsl:attribute name="Filename" select=".[contains(@LayoutPath,'Filename')]/@Value"/> 
       <xsl:attribute name="URI" select=".[contains(@LayoutPath,'URI')]/@Value"/> 

   <xsl:attribute name="Filename" select="current-group()[contains(@LayoutPath,'Filename')]/@Value"/> 
       <xsl:attribute name="URI" select="current-group()[contains(@LayoutPath,'URI')]/@Value"/> 
+0

謝謝你的作品。 – DanielCSD