在XSLT中为空值添加条件语句

时间:2015-11-10 05:55:16

标签: json xml wordpress xslt

我正在使用以下XSL解析来自Wordpress博客的RSS提要(这是相关代码段)以抽出JSON。当以下每个项目都匹配时,一切正常,花花公子。但是我有一个新案例,我试图从一个单独的Wordpress博客中提取,该博客没有设置类别,这会生成一个空值,从而破坏了我的脚本。我在下面注入了一个有效的条件声明。有什么想法吗?

 <xsl:template match="item">
{ "title": "<xsl:value-of select="title"/>" 
    ,"link": "<xsl:value-of select="link"/>" 
    ,"date": "<xsl:value-of select="date-converter:getDateFormat(string(pubDate) , 'mmmm d, yyyy')"/>" 
    ,"category": "<xsl:apply-templates select="category"/>" 
    ,"description": "<xsl:call-template name="replace">
            <xsl:with-param name="text">
                <xsl:value-of select="normalize-space(description/node())"/>
            </xsl:with-param>
            <xsl:with-param name="replace">"</xsl:with-param>
            <xsl:with-param name="by">\"</xsl:with-param>
        </xsl:call-template>"       
}
<xsl:if test="not(position() = last())">
    ,
</xsl:if>

这里是来自RSS的输入数据样本。您将注意到null类别,对于其他RSS提要,它将类似于&#34; Events&#34;但是对于这个博客,由于他们设置它的方式没有类别(顺便说一句,这个博客不是我的,所以我不能改变它的结构)。

<data>

        [

{ "title": "Inventors Forum: Speakers Series" 
    ,"link": "http://innovation.uci.edu/event/inventors-forum-speakers-series/" 
    ,"date": "November 13, 2015" 
    ,"category": "" 
    ,"description": "About Inventors Forum We are an organization of inventors and those who help inventors. We invite you to join us. Mission Statement Inventors Forum is dedicated to assisting and educating early-stage inventors and to providing the environment necessary to safely bring new products ideas to the marketplace. What We Are We are a 501(c)3 nonprofit … <a href=\"http://innovation.uci.edu/event/inventors-forum-speakers-series/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Inventors Forum: Speakers Series</span> <span class=\"meta-nav\">→</span></a>"       
}

    ,

{ "title": "Golden Seeds SoCal Overview & Reception" 
    ,"link": "http://innovation.uci.edu/event/golden-seeds-socal-overview-reception/" 
    ,"date": "November 16, 2015" 
    ,"category": "" 
    ,"description": "Golden Seeds was founded ten years ago, with the goal of raising substantial capital for women-led start-ups. During that time, they have met thousands of companies and have invested $77 million in over 100 companies. Golden Seeds is widely recognized as a leading voice in the movement to propel women entrepreneurs. At the Golden Seeds … <a href=\"http://innovation.uci.edu/event/golden-seeds-socal-overview-reception/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Golden Seeds SoCal Overview & Reception</span> <span class=\"meta-nav\">→</span></a>"       
}

    ,

{ "title": "NHLBI Innovation Conference – West" 
    ,"link": "http://innovation.uci.edu/event/nhlbi-innovation-conference-west/" 
    ,"date": "November 17, 2015" 
    ,"category": "" 
    ,"description": "NHLBI’s Office of Translational Alliances and Coordination (OTAC) hosts the NHLBI Innovation Conference to connect NHLBI-funded companies, investors, strategic partners, and business leaders from the biotech, medical device, and pharmaceutical industries. The Conference showcases NHLBI SBIR and STTR awardees who are developing the next generation of products to diagnose and treat heart, lung, blood, and … <a href=\"http://innovation.uci.edu/event/nhlbi-innovation-conference-west/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">NHLBI Innovation Conference – West</span> <span class=\"meta-nav\">→</span></a>"       
}

    ,

{ "title": "1 Million Cups Irvine" 
    ,"link": "http://innovation.uci.edu/event/1-million-cups-irvine-7/" 
    ,"date": "December 2, 2015" 
    ,"category": "" 
    ,"description": "1 Million Cups is a unique event every Wednesday morning from 8am-9am at The Cove, where we provide free coffee and tea. The two startups give a 6 minute presentation spot, then give the live audience of 50+ people  20 minutes to ask questions, give feedback, and act as a focus group with the intention … <a href=\"http://innovation.uci.edu/event/1-million-cups-irvine-7/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">1 Million Cups Irvine</span> <span class=\"meta-nav\">→</span></a>"       
}

    ,

{ "title": "1 Million Cups Irvine" 
    ,"link": "http://innovation.uci.edu/event/1-million-cups-irvine-8/" 
    ,"date": "December 9, 2015" 
    ,"category": "" 
    ,"description": "1 Million Cups is a unique event every Wednesday morning from 8am-9am at The Cove, where we provide free coffee and tea. The two startups give a 6 minute presentation spot, then give the live audience of 50+ people  20 minutes to ask questions, give feedback, and act as a focus group with the intention … <a href=\"http://innovation.uci.edu/event/1-million-cups-irvine-8/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">1 Million Cups Irvine</span> <span class=\"meta-nav\">→</span></a>"       
}

    ,

{ "title": "1 Million Cups Irvine" 
    ,"link": "http://innovation.uci.edu/event/1-million-cups-irvine-10/" 
    ,"date": "December 16, 2015" 
    ,"category": "" 
    ,"description": "1 Million Cups is a unique event every Wednesday morning from 8am-9am at The Cove, where we provide free coffee and tea. The two startups give a 6 minute presentation spot, then give the live audience of 50+ people  20 minutes to ask questions, give feedback, and act as a focus group with the intention … <a href=\"http://innovation.uci.edu/event/1-million-cups-irvine-10/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">1 Million Cups Irvine</span> <span class=\"meta-nav\">→</span></a>"       
}

        ]

    </data>

1 个答案:

答案 0 :(得分:0)

下面的解决方案检查“类别”是否为空/不存在。顺便说一下,也是日期。

<xsl:template match="item">
{ "title": "<xsl:value-of select="title"/>"
    ,"link": "<xsl:value-of select="link"/>"
    ,"date": "<xsl:if test="date != ''"><xsl:value-of select="date-converter:getDateFormat(string(pubDate) , 'mmmm d, yyyy')"/></xsl:if>"
    ,"category": "<xsl:if test="category != ''"><xsl:apply-templates select="category"/></xsl:if>"
    ,"description": "<xsl:call-template name="replace">
            <xsl:with-param name="text">
                <xsl:value-of select="normalize-space(description/node())"/>
            </xsl:with-param>
            <xsl:with-param name="replace">"</xsl:with-param>
            <xsl:with-param name="by">\"</xsl:with-param>
        </xsl:call-template>"
}
<xsl:if test="not(position() = last())">
    ,
</xsl:if>
</xsl:template>

如果这不是您所希望的行为,您可以在此处找到其他修改:https://stackoverflow.com/a/825869/1305969