2016-05-24 9 views
0

のノードを作成します。XSLT - 私は、次のXMLファイル持っている別の文脈から

<config> 
    <files> 
     <file uuid="uuid-file1"> 
      <name>File1</name> 
     </file> 
     <file uuid="uuid-file2"> 
      <name>File2</name> 
     </file> 
     <file uuid="uuid-file3"> 
      <name>File3</name> 
     </file> 
     <file uuid="uuid-file4"> 
      <name>File3</name> 
     </file> 
    </files> 
    <folders> 
     <folder uuid="root" /> 
     <folder uuid="folder1"> 
      <member ref="uuid-file1" /> 
     </folder> 
     <folder uuid="folder2"> 
      <member ref="uuid-file2" /> 
     </folder> 
    </folders> 
</config> 

すなわち= FILE1のfolder1に参照されているとfile2は、フォルダ2に参照されています。 file3とfile4はフォルダにありません。

私の問題:

私はすべての「ファイル」を参照して、フォルダ内で参照されていない彼らのためにフォルダ「ルート」の参照を作成するためのXSLT変換を作成します。

例(FILE3とFILE4がフォルダに含まれていない):

<config> 
    <files> 
     <file uuid="uuid-file1"> 
      <name>File1</name> 
     </file> 
     <file uuid="uuid-file2"> 
      <name>File2</name> 
     </file> 
     <file uuid="uuid-file3"> 
      <name>File3</name> 
     </file> 
     <file uuid="uuid-file4"> 
      <name>File4</name> 
     </file> 
    </files> 
    <folders> 
     <folder uuid="root" > 
      <member ref="uuid-file3" /> 
      <member ref="uuid-file4" /> 
     </folder> 
     <folder uuid="folder1"> 
      <member ref="uuid-file1" /> 
     </folder> 
     <folder uuid="folder2"> 
      <member ref="uuid-file2" /> 
     </folder> 
    </folders> 
</config> 

私は「の-各」ループでそれをやろうが、私は、フォルダに新しいノードを作成する方法がわからない「ルート「私は、 "ファイル" ノードを参照するときに...

私は

よろしく、 Zido

+0

あなたが試したXSLTを表示できますか?そしてそれの問題?それは助けを得る可能性が高くなります... –

答えて

0

:-)あなたの助けを必要とし、このようにそれを試してみてください。

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="folder" match="folder" use="member/@ref" /> 

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

<xsl:template match="folder[@uuid='root']"> 
    <xsl:copy> 
     <xsl:for-each select="/config/files/file[not(key('folder', @uuid))]"> 
      <member ref="{@uuid}"/> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

これは我々が(その@uuid値は文字通りの文字列「ルート」であることによって、この例では)「ルート」フォルダを特定する方法を知っている前提としています。あなたの質問のこの部分はそれほど明確ではありません。

+0

右:フォルダ "ルート"は、 'root'に等しいuuidによって識別されます。 – zido71

+0

ありがとうございました!それは完全に動作します。ツリー全体をコピーするために、を追加しました。 – zido71

+0

@ zido71 * identity transform *テンプレートは "ツリー全体をコピーする"。 –

関連する問題