XSLT格式化Wordpress WXR XML,以通过Feed导入Drupal

乔恩·米尔斯

我正在尝试使用XSLT格式化Wordpress WXR文件,以便可以将其导入Drupal。

我知道可以导入W​​XR文件的Drupal模块,但是我需要Feeds模块可以提供的灵活性,因为导入的数据将针对不同的内容类型进行导入,并将图像和其他附件拉入新创建的Drupal中页面。考虑到这一点,标准的WordPress Migrate不会削减它。

因此,WXR格式items在Feed中将Wordpress帖子和附件分开,并使用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的新手,并且已经阅读了一些有关身份转换的文章,我认为这是正确的方向,但是我只是没有经验来满足我的需求,我们将不胜感激。

乔恩·米尔斯

看来我已经设法解决了一个问题。

我使用了许多索引来组织附件。在进一步检查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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章