我需要修剪XML输出,并需要一些建议。我目前的产出是:
<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>我的问题是:
<ParentID>20064</ParentID>。<CategoryID>Lounge Chairs</CategoryID>值中的空格,使其看起来类似于<CategoryID>LoungeChairs</CategoryID>你能告诉我怎么做吗?我的XML模板是,
<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>发布于 2015-12-18 04:16:35
当对应的xsl:if元素没有<ParentID>时,可以使用简单的object测试来防止将parent_id添加到输出object:
<xsl:if test="parent_id">
<ParentID><xsl:value-of select="parent_id"/></ParentID>
</xsl:if>或者,如果object元素可能具有没有值的parent_id子元素,而且您也不希望在这种情况下输出ParentID元素:
<xsl:if test="normalize-space(parent_id)">
<ParentID><xsl:value-of select="parent_id"/></ParentID>
</xsl:if>发布于 2015-12-18 09:57:20
以下语法将删除字符串中的空格:
<CategoryID>
<xsl:value-of select="php:functionString('str_replace',' ','',cats/cat/name)" />
</CategoryID>https://stackoverflow.com/questions/34338552
复制相似问题