2013-07-18 38 views
1

我想使用XSLT格式化一個Wordpress WXR文件,所以我可以將它導入到Drupal。XSLT格式的Wordpress WXR XML通過飼料導入到Drupal

我知道模塊爲Drupal將導入WXR文件,但我需要的靈活性,飼料模塊可以給導入的數據將導入不同的內容類型,我會拉圖像和其他附件到新創建的Drupal頁面。考慮到這一點,標準的WordPress Migrate不會削減它。

因此,WXR格式將Wordpress帖子和附件作爲單獨的items作爲Feed內的鏈接,並使用ID鏈接帖子附件。附件可以是圖像,文件(pdf,doc等),可在xpath wp:postmeta/wp:meta_key找到,並具有值_thumbnail_id,_wp_attached_file

我想要做的是從類型附件中的各種節點,並把它們放在其中id將它們鏈接在一起

要轉換的xml片段...第一項是post第二項是attachment。該

<item> 
    <title>Some groovy title</title> 
    <link>http://example.com/groovy-example</link> 
    <wp:post_id>2050</wp:post_id> 
    <wp:post_type>page</wp:post_type> 
    ... 
    ... 
    ... 
    <wp:postmeta> 
     <wp:meta_key>_thumbnail_id</wp:meta_key> 
     <wp:meta_value>566</wp:meta_value> 
    </wp:postmeta> 
</item> 
... 
... 
... 
<item> 
    <title>My fantastic attachment</title> 
    <link>http://www.example.com/fantastic-attachment</link> 
    <wp:post_id>566</wp:post_id> 
    <wp:post_type>attachment</wp:post_type> 
    ... 
    ... 
    ... 
    <wp:attachment_url>http://www.example.com/wp-content/uploads/2012/12/fantastic.jpg</wp:attachment_url> 
    <wp:postmeta> 
     <wp:meta_key>_wp_attached_file</wp:meta_key> 
     <wp:meta_value>2012/12/fantastic.jpg</wp:meta_value> 
    </wp:postmeta> 
</item> 

變換後,我想

<item> 
    <title>Some groovy title</title> 
    <link>http://example.com/groovy-example</link> 
    <wp:post_id>2050</wp:post_id> 
    <wp:post_type>page</wp:post_type> 
    ... 
    ... 
    ... 
    <wp:postmeta> 
     <wp:meta_key>_thumbnail_id</wp:meta_key> 
     <wp:meta_value>566</wp:meta_value> 
     <wp:meta_url>http://www.example.com/wp-content/uploads/2012/12/fantastic.jpg</wp:attachment_url> 
    </wp:postmeta> 


</item> 

也許,有更好的方法嗎?也許合併後和附件的id創建節點之間的鏈接?

我是XSLT的新手,已經閱讀了一些關於身份轉換的文章,我認爲這是正確的方向,但我沒有經驗來拉我需要的東西,我將不勝感激。

回答

0

看起來我已經設法解決了一個問題。

我使用了許多索引來組織附件。我的需求變化不大的XML的進一步檢查,因爲有

我改變了我的輸出結果是在格式...

<item> 
    <title>Some groovy title</title> 
    <link>http://example.com/groovy-example</link> 
    <wp:post_id>2050</wp:post_id> 
    <wp:post_type>page</wp:post_type> 
    ... 
    ... 
    ... 
    <thumbnail> 
     <title>Spaner</title> 
     <url>http://www.example.com/wp-content/uploads/2012/03/spanner.jpg</url> 
    </thumbnail> 
    <attachments> 
     <attachment> 
      <title>Fixing your widgets: An idiots guide</title> 
      <url>http://www.example.com/wp-content/uploads/2012/12/fixiing-widgets.pdf</url> 
     </attachment> 
     <attachment> 
      <title>Do It Yourself Trepanning</title> 
      <url>http://www.example.com/wp-content/uploads/2013/04/trepanning.pdf</url> 
     </attachment> 
    </attachments> 
</item> 

因此,使用下面的XSL給了我想要的結果。索引條件確保我選擇了正確的文件。

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:wp="http://wordpress.org/export/1.2/"> 

    <xsl:output indent="yes" cdata-section-elements="content"/> 

    <!-- Setup indexes --> 

    <!-- Index all main posts --> 
    <xsl:key 
     name="mainposts" 
     match="*/item[wp:post_type[text()='post']]" 
     use="wp:post_id" /> 

    <!-- Index all sub posts (posts within posts)--> 
    <xsl:key 
     name="subposts" 
     match="*/item[wp:post_type[text()='post'] and category[@nicename = 'documents']]" 
     use="category[@domain = 'post_tag']" /> 

    <!-- Index all image thumbs --> 
    <xsl:key 
     name="images" 
     match="*/item[wp:post_type[text()='attachment'] and wp:postmeta/wp:meta_key[text()='_wp_attachment_metadata']]" 
     use="wp:post_parent" /> 

    <!-- Index all files (unable to sort members file at the moment)--> 
    <xsl:key 
     name="attachments" 
     match="*/item[wp:post_type[text()='attachment'] and not(wp:postmeta/wp:meta_key = '_wp_attachment_metadata')]" 
     use="wp:post_parent" /> 

    <xsl:key 
     name="thumbnails" 
     match="*/item[wp:post_type[text()='attachment']]" 
     use="wp:post_id" /> 

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

    <xsl:template match="*/item/wp:post_parent[text()= 0]"> 
     <wp:post_parent> 
      <xsl:value-of select="." /> 
     </wp:post_parent> 

     <xsl:for-each select="key('thumbnails', ../wp:postmeta[wp:meta_key[text()='_thumbnail_id']]/wp:meta_value)"> 
      <thumbnail> 
       <title><xsl:value-of select="title" /></title> 
       <url><xsl:value-of select="wp:attachment_url" /></url> 
      </thumbnail> 
     </xsl:for-each> 

     <xsl:for-each select="key('subposts', ../category[@domain = 'post_tag'])"> 
      <attachments> 

       <xsl:for-each select="key('images', wp:post_id)"> 
        <file> 
         <title><xsl:value-of select="title" /></title> 
         <url><xsl:value-of select="wp:attachment_url" /></url> 
        </file> 
       </xsl:for-each> 

       <xsl:for-each select="key('attachments', wp:post_id)"> 
        <file> 
         <title><xsl:value-of select="title" /></title> 
         <url><xsl:value-of select="wp:attachment_url" /></url> 
        </file> 
       </xsl:for-each> 

      </attachments> 
     </xsl:for-each> 

    </xsl:template>