如何判断XML标签是否包含值并进行相应操作
How to check wether the XML tag contain value or not and perform the operation accordingly
我有一个 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
也为空。
请帮我解决这个问题。
尝试以下操作:
XSLT 1.0:
<xsl:template match="WeightUnits">
<weightUnits>
<xsl:if test="../Weight!=''">
<xsl:value-of select="'lbs'"/>
</xsl:if>
</weightUnits>
</xsl:template>
XSLT 2.0:
<xsl:template match="WeightUnits">
<weightUnits>
<xsl:value-of select="if(../Weight!='') then('lbs') else('')"/>
</weightUnits>
</xsl:template>
这是一个根本不使用任何 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>
我有一个 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
也为空。
请帮我解决这个问题。
尝试以下操作:
XSLT 1.0:
<xsl:template match="WeightUnits">
<weightUnits>
<xsl:if test="../Weight!=''">
<xsl:value-of select="'lbs'"/>
</xsl:if>
</weightUnits>
</xsl:template>
XSLT 2.0:
<xsl:template match="WeightUnits">
<weightUnits>
<xsl:value-of select="if(../Weight!='') then('lbs') else('')"/>
</weightUnits>
</xsl:template>
这是一个根本不使用任何 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>