2013-03-19 7 views
5

Attualmente sto imparando XSL e ho una domanda riguardante i riferimenti incrociati. Il mio file XML di destinazione è strutturato in questo modo:Riferimento incrociato XSL

<XML> 
    <Model> 
     <packedElement typ="class" id="1"/> 
     <packedElement typ="class" id="2"/> 
     <packedElement typ="class" id="3"/> 
    </Model> 
    <Elements> 
     <Element idref="1"> 
      <Attributes comment="comment 1."/> 
     </Element> 
     <Element idref="2"> 
      <Attributes comment="comment 2."/> 
     </Element> 
     <Element idref="3"> 
      <Attributes comment="comment 3."/> 
     </Element> 
    </Elements> 
</XML> 

Voglio collegare id = idref. Il mio obiettivo è elencare tutti gli elementi imballati e stampare i loro commenti. Potete aiutarmi ragazzi?

Ho provato a risolverlo con la funzione key-funktion, ma non ero molto riuscito.

Edit:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> 
<xsl:output method="xml" encoding="UTF-8"/> 

<xsl:key name="CommentK" match="Element" use="@idref"/> 


<xsl:template match="XML"> 
<XML> 
<xsl:apply-templates/> 
</XML> 
</xsl:template> 

<xsl:template name="Start" match="packedElement"> 
<xsl:variable name="TEST" select="@id"/> 
<xsl:variable name="Comment"> 
<xsl:call-template name="FindComment"> 
<xsl:with-param name="test2" select="@id"/> 
</xsl:call-template> 
</xsl:variable> 
<content comment="{$Comment}" id ="{@id}" test="{$TEST}"></content> 
</xsl:template> 

<xsl:template name="FindComment"> 
<xsl:param name="test2"/> 

<xsl:for-each select="key('CommentK', '$test2')"> 

<xsl:value-of select="Attributes/@comment"/> 

</xsl:for-each> 

</xsl:template> 

</xsl:stylesheet> 

versione XSLT è 2.0. (Btw qualcuno può dirmi la differenza tra il .xslt e .xsl?)

+0

XSLT 1.0 o 2.0? –

+0

Che aspetto ha il tuo tentativo? Come volevi che fosse il risultato? – JLRishe

+0

Penso che il tuo tentativo avrebbe quasi funzionato, tranne che hai usato ''$ test2'' che è il valore_string_" $ test2 ". Dovrebbe essere stato solo 'key ('CommentK', $ test2)'. Penso che il 'FindComment' e' for-each' siano overkill però. La risposta di Martin Honnen è buona. – JLRishe

risposta

6

Prova

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0"> 

<xsl:output indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="el-by-idref" match="Elements/Element" use="@idref"/> 

<xsl:template match="XML"> 
    <xsl:copy> 
    <xsl:apply-templates select="Model/packedElement"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Model/packedElement"> 
    <content comment="{key('el-by-idref', @id)/Attributes/@comment}" id="{@id}" test="{@id}"/> 
</xsl:template> 



</xsl:stylesheet> 

in questo modo si ottiene

<XML> 
    <content comment="comment 1." id="1" test="1"/> 
    <content comment="comment 2." id="2" test="2"/> 
    <content comment="comment 3." id="3" test="3"/> 
</XML> 
+0

Wow .. grazie per l'aiuto molto veloce !! Questo è quello che ho cercato. Era un file di destinazione molto semplice, ora provo a scrivere il codice per il big-xml. Grazie! – gs3rr4