首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XML:如果节点为空,则不要在输出中显示任何内容

XML:如果节点为空,则不要在输出中显示任何内容
EN

Stack Overflow用户
提问于 2015-12-17 15:42:02
回答 2查看 127关注 0票数 0

我需要修剪XML输出,并需要一些建议。我目前的产出是:

代码语言:javascript
复制
<Product>
  <ProductUniqueID>16772</ProductUniqueID>
  <Name>Chill Armchair In Leather Px30 Graphite 063 With</Name>
  <ImageUrl>https://www.example.com/media/catalog/product/1/0/10157.jpg</ImageUrl>
  <Price>1595.0000</Price>
  <ParentID>20064</ParentID>
  <ProductUrl>https://www.example.com/chill-armchair.html</ProductUrl>
  <CategoryID>Lounge Chairs</CategoryID>
</Product>

我的问题是:

  1. 我的一些产品没有父ID,当产品中没有父ID时,我想删除节点<ParentID>20064</ParentID>
  2. 我希望删除<CategoryID>Lounge Chairs</CategoryID>值中的空格,使其看起来类似于<CategoryID>LoungeChairs</CategoryID>

你能告诉我怎么做吗?我的XML模板是,

代码语言:javascript
复制
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
        <xsl:template match="/">
        <Feed>
        <Products>
        <xsl:for-each select="objects/object">
                    <Product>
                         <ProductUniqueID><xsl:value-of select="entity_id"/></ProductUniqueID>
                         <Name><xsl:value-of select="name"/></Name>
                         <ImageUrl><xsl:value-of select="images/image/url"/></ImageUrl>    
                         <Price><xsl:value-of select="price"/></Price>                                                                               
                         <ParentID><xsl:value-of select="parent_id"/></ParentID>
                        <ProductUrl><xsl:text>https://www.example.com/</xsl:text>
                        <xsl:choose>
                        <xsl:when test="string(parent_item/url_key)"><xsl:value-of select="parent_item/url_key" /></xsl:when>
                        <xsl:otherwise>
                        <xsl:value-of select="url_key" />
                        </xsl:otherwise>
                        </xsl:choose>
                        <xsl:text>.html</xsl:text>
                        </ProductUrl>
                         <CategoryID><xsl:value-of select="cats/cat/name"/></CategoryID>
                    </Product>
                   </xsl:for-each>
                   </Products>
                </Feed>
        </xsl:template>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-18 04:16:35

当对应的xsl:if元素没有<ParentID>时,可以使用简单的object测试来防止将parent_id添加到输出object

代码语言:javascript
复制
<xsl:if test="parent_id">
    <ParentID><xsl:value-of select="parent_id"/></ParentID>
</xsl:if>

或者,如果object元素可能具有没有值的parent_id子元素,而且您也不希望在这种情况下输出ParentID元素:

代码语言:javascript
复制
<xsl:if test="normalize-space(parent_id)">
    <ParentID><xsl:value-of select="parent_id"/></ParentID>
</xsl:if>
票数 1
EN

Stack Overflow用户

发布于 2015-12-18 09:57:20

以下语法将删除字符串中的空格:

代码语言:javascript
复制
<CategoryID>
<xsl:value-of select="php:functionString('str_replace',' ','',cats/cat/name)" />
</CategoryID>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34338552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档