我有一个XML,类似于:
<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<Weight>15 kg</Weight>
<WeightUnits></WeightUnits>
</COLLECTION>我想表演KG到LBS
为此,我写到:
<xsl:template match="Weight">
<weight>
<xsl:value-of
select="translate(., translate(., '.0123456789', ''), '') div 0.45359237" />
</weight>
</xsl:template>
<xsl:template match="WeightUnits">
<weightUnits>lbs</weightUnits>
</xsl:template>一切都很好:
我的问题是如何检查数据是否存在于<Weight>中
也就是说,如果Weight的值存在,并且只有weightUnits包含LBS,如果Weight是空的,则weightUnits也是空的。
请帮我解决这个问题。
发布于 2016-10-18 02:56:24
这里有一个解决方案,它在所有上都不使用任何XSLT条件运算符
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Weight">
<weight><xsl:apply-templates/></weight>
</xsl:template>
<xsl:template match="Weight/text()[normalize-space()]">
<xsl:value-of
select="translate(., translate(., '.0123456789', ''), '') div 0.45359237" />
</xsl:template>
<xsl:template match="WeightUnits">
<weightUnits><xsl:apply-templates
select="../Weight[normalize-space()]" mode="lbs"/></weightUnits>
</xsl:template>
<xsl:template match="*" mode="lbs">lbs</xsl:template>
</xsl:stylesheet>当此转换应用于所提供的XML文档时,则为。
<COLLECTION>
<Weight>15 kg</Weight>
<WeightUnits></WeightUnits>
</COLLECTION>想要的,正确的结果产生
<COLLECTION>
<weight>33.06933932773163</weight>
<weightUnits>lbs</weightUnits>
</COLLECTION>当对以下XML文档应用相同的转换时, (<weight>为空):
<COLLECTION>
<Weight></Weight>
<WeightUnits></WeightUnits>
</COLLECTION>再一次想要的,正确的结果产生
<COLLECTION>
<weight/>
<weightUnits/>
</COLLECTION>发布于 2016-10-17 13:28:08
尝试以下几点:
XSLT1.0:
<xsl:template match="WeightUnits">
<weightUnits>
<xsl:if test="../Weight!=''">
<xsl:value-of select="'lbs'"/>
</xsl:if>
</weightUnits>
</xsl:template>XSLT2.0:
<xsl:template match="WeightUnits">
<weightUnits>
<xsl:value-of select="if(../Weight!='') then('lbs') else('')"/>
</weightUnits>
</xsl:template>https://stackoverflow.com/questions/40086554
复制相似问题