Google产品Feed xml的XSLT转换

时间:2013-08-14 14:43:27

标签: xml xslt xml-parsing

我有一个Google Product Feed XML,我需要执行XSLT转换才能转换为与联属网络兼容的新格式。我可以提取标题,描述和链接,但无法弄清楚如何引用g:id或g:condition等值。非常感谢任何帮助。

这是源XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
    <item>
        <g:id>B1005778</g:id>
        <title>Butterfly Twists Eve Black Women Ballerinas</title>
        <link>http://shopping.indiatimes.com/fashion/ballerinas/butterfly-twists-eve-black-women-ballerinas/41904/p_B1005778</link>
        <g:price>1530.000 INR</g:price>
        <description>A pair of black ballerinas for women from Butterfly Twists  Made of leatherite  these stylish ballerinas are the perfect pick for all those who want to stay on the top of the style meter without hav</description>
        <g:condition>new</g:condition>
        <g:image_link>http://shopping.indiatimes.com/images/products/medium/B1005778.jpg</g:image_link>
        <g:brand>Butterfly Twists</g:brand>
        <g:product_type>Fashion</g:product_type>
        <g:google_product_category>Fashion &amp;gt; Women &amp;gt; Footwears &amp;gt; Ballerinas</g:google_product_category>
    </item>
</channel>

这是我的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:omg="http://feeds.omgadmin.co.uk/feeds/ns/1.0/" xmlns:rss="http://feeds.omgeu.com/ns/1.0/">

<xsl:param name="pubDate"/>
<xsl:param name="channelTitle" select="Test"/>
<xsl:param name="channelLink"/>
<xsl:param name="channelDescription"/>
<xsl:param name="channelLanguage"/>
<xsl:param name="channelCopyright"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>

<xsl:template match="/">

<rss version="2.0">
<channel>
    <pubDate><xsl:value-of select="$pubDate"/></pubDate>
    <title><xsl:value-of select="$channelTitle"/></title>
    <link><xsl:value-of select="$channelLink"/></link>
    <description><xsl:value-of select="$channelDescription"/></description>
    <language><xsl:value-of select="$channelLanguage"/></language>
    <copyright><xsl:value-of select="$channelCopyright"/></copyright>
    <omg:processCount><xsl:value-of select="count(//product)"/>    </omg:processCount>

    <xsl:apply-templates/>

</channel>
</rss>
</xsl:template>

<xsl:template name="itemTemplate" match="item">
    <item>
        <!--<omg:merchantrank>0</omg:merchantrank>-->
        <omg:pid>10091</omg:pid>
        <title><xsl:value-of select="title"/></title>
        <description><xsl:value-of select="description"/></description>
        <link><xsl:value-of select="link"/><![CDATA[?aff=in.affiliate.1230441.{aid}.shoes.aff&utm_source=OMGPM.COM1&utm_medium=dc-clicktracker&utm_campaign={aid}&utm_content=shoes]]></link>
        <omg:imageurlsmall></omg:imageurlsmall>
        <omg:imageurlmedium><xsl:value-of select="productimage"/></omg:imageurlmedium>
        <omg:imageurllarge></omg:imageurllarge>
        <omg:price><xsl:value-of select="actualprice"/></omg:price>
        <omg:currency>INR</omg:currency>

        <omg:sku><xsl:value-of select="g:id"/></omg:sku>


        <omg:startdate/>
        <!--<omg:enddate></omg:enddate>-->

        <omg:categories>
            <xsl:call-template name="itemCategories"/>
        </omg:categories>

        <omg:attributes>
                <xsl:call-template name="itemAttributes"/>
        </omg:attributes>
    </item>
</xsl:template>

问题在于:

<omg:sku><xsl:value-of select="g:id"/></omg:sku>

提前致谢

2 个答案:

答案 0 :(得分:1)

需要在样式表中声明g命名空间。尝试将样式表元素更改为:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:omg="http://feeds.omgadmin.co.uk/feeds/ns/1.0/" xmlns:rss="http://feeds.omgeu.com/ns/1.0/" xmlns:g="http://base.google.com/ns/1.0">

然后g:id选择应该有效。

答案 1 :(得分:0)

我认为你只需要将google rss命名空间添加到样式表中:

xmlns:g =“http://base.google.com/ns/1.0”

将其添加到样式表顶部的xsl:stylesheet元素。