2016-08-15 15 views
1

1に参加することができますスキーマは、どのように私は</p> <p>最初のファイルには、次のように<code>Song.xml</code>で参加する必要がある2つの<code>.xml</code>のファイルを持っている2 XML XSL

<ns2:gernes xmlns="http://www.w3.org/2001/XMLSchema/playlist" xmlns:ns2="https://xml.netbeans.org/schema/genses"> 
    <ns2:gerne> 
     <GerneCode>1</GerneCode> 
     <GerneName>Pop</GerneName>   
     <Image>img-pop.jpg</Image> 
    </ns2:gerne> 
</ns2:gerne> 

私はそのすべての曲そのミリアンペアのためGerneNameを追加するために、XSL内部でこれら.xmlファイルを結合したいですGerne.xmlのGerneNameをtchしてください。取得しようとして

結果イムは、次のようにする必要があります:

<Songs> 
     <Song> 
     <SongID>1</SongID> 
     <SongName>We dont talk anymore</SongName> 
     <Author>M-TP</Author> 
     <UploadBy>admin</UploadBy> 
     <GerneName>Pop</GerneName> 
     <GerneCode>1</GerneCode> 
     </Song> 
</Songs> 

誰もがこれで私を助けることができますか?たとえば、この問題をどのように見極める必要がありますか?

+0

[document](http://www.w3schools.com/xsl/func_document.asp)関数を使って 'Gerne.xml'を読んで、' GerneCode'、 'GerneName'を取得し、 'GerneCode'は現在の要素の' GerneCode'を 'Songs.xml'にマッチさせ、' GerneCode'、 'GerneName'をあなたの現在の要素の' Songs.xml'にコピーします。 xpath式とTransformerFactoryについては、[XpathFactory](https://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/XPathFactory.html)を使用できます。https://docs.oracle .com/javase/7/docs/api/javax/xml/transform/TransformerFactory.html)を使用しています。 – SomeDude

+0

XSLT 2.0を使用できますか? –

答えて

0

あなたが行うことができ、あなたは、XSLT 1.0に制限されていると仮定:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:pl="http://www.w3.org/2001/XMLSchema/playlist" 
xmlns:ns2="https://xml.netbeans.org/schema/genses" 
exclude-result-prefixes="pl ns2"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:variable name="lookup-path" select="'Gerne.xml'" /> 
<xsl:key name="genre" match="ns2:gerne" use="pl:GerneCode" /> 

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

<xsl:template match="GerneCode"> 
    <xsl:variable name="code" select="." /> 
    <!-- switch context to the other file --> 
    <xsl:for-each select="document($lookup-path)"> 
     <GerneCode> 
      <xsl:value-of select="key('genre', $code)/pl:GerneName" /> 
     </GerneCode> 
    </xsl:for-each> 
    <xsl:copy-of select="."/> 
</xsl:template> 

</xsl:stylesheet> 

これはSong.xmlファイルを処理するために、あなたのXSLTプロセッサを言っていると仮定すると、パラメータとしてGerne.xmlファイルへのパスを渡しています。

投稿したGerne.xmlの文書は整形式ではありません(</ns2:gernes>終了タグなし)。


ところで、正しい用語は「ジャンル」または「gense」ではありません。

+0

ありがとうございます。この解決策は機能します。私の悪い英語のために申し訳ありません、私のボキャブラリーに問題があります。 –

+0

@BeansVănQuie質問に答えた場合は、回答を受け入れて閉じてください。 –

関連する問題