【问题标题】:Adding a condititnal statment for null value in XSLT在 XSLT 中为空值添加条件语句
【发布时间】:2015-11-10 05:55:15
【问题描述】:

我正在使用以下 XSL(这是相关的 sn-p)解析来自 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 的输入数据示例。你会注意到空类别,对于其他 RSS 提要,它类似于“事件”,但对于这个博客,由于它们设置它的方式,没有类别(顺便说一下,这个博客不是我的,所以我不能改变它的结构)。

<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>

【问题讨论】:

  • 请发布一些输入 XML 代码,以便我们重现您的错误。
  • 我将数据添加到我的原始帖子中。

标签: json xml wordpress xslt


【解决方案1】:

下面的解决方案检查“类别”是否为空/不存在。顺便说一句,还有约会。

<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

【讨论】:

  • 谢谢。这和其他线程的链接一样有用。我正在搜索有关此主题的先前讨论,但从未出现过!
猜你喜欢
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多