为什么 xsl:when 没有按预期工作? [英] Why is xsl:when not working as expected?
问题描述
我是 XSLT 的初学者,我对如何使用 xsl:when
有点困惑.请在下面找到我的示例 XSLT 代码,其中包含预期输出和我从 XSLT 获得的实际输出.您能否提出您的解决方案并解释我在使用 xsl:when
时做错了什么.
XML
XSLT
<xsl:template match="source"><xsl:when test="bold"><xsl:元素名称="p"><xsl:元素名称="b"><xsl:value-of select="bold"></xsl:value-of></xsl:element></xsl:element></xsl:when><xsl:when test="red"><xsl:元素名称="p"><xsl:value-of select="red"></xsl:value-of></xsl:element></xsl:when><xsl:when test="斜体"><xsl:元素名称="p"><xsl:element name="i"><xsl:value-of select="italic"></xsl:value-of></xsl:element></xsl:element></xsl:when><xsl:否则><xsl:元素名称="p"><xsl:apply-templates/></xsl:element></xsl:否则></xsl:选择></xsl:模板></xsl:stylesheet>
预期输出
<p><b>你好,世界.</b></p><p><i>很好.</i></p><p style="color:red;">我是 </p>
实际输出
<p><b>你好,世界.</b></p>
如果可能,您能否展示我们如何在xsl:选择?
要使用 xsl:choose
实现这一点,您可以执行以下操作:
<div><xsl:apply-templates/>
</xsl:模板><xsl:template match="source/*"> 请注意,此处使用 这意味着每个元素都测试自身(因此使用 请记住,仅仅因为它是可能的,并不意味着它是最好的方法. I am a beginner to the XSLT and I am a bit confused on how to use XML XSLT EXPECTED OUTPUT ACTUAL OUTPUT
If possible can you show how we can Implement this expected output in
xsl:choose? To implement this using Note that here the template using This means that each element tests itself (hence the use of the Keep in mind that just because it is possible, it doesn't mean it's the best way to do it. 这篇关于为什么 xsl:when 没有按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!xsl:choose
的模板与 source
的实际子元素匹配 - 与您在父 source
水平.self
轴)和自身.与您的尝试不同,如果 source
的 any 孩子是 <bold>
.xsl:when
. Please find my sample XSLT code below with expected output and the actual output I am getting from the XSLT. Could you please suggest your solution and explain what I am doing incorrect while using xsl:when
.<source>
<bold>Hello, world.</bold>
<italic>fine.</italic>
<red>I am </red>
</source>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="source">
<xsl:choose>
<xsl:when test="bold">
<xsl:element name="p">
<xsl:element name="b">
<xsl:value-of select="bold"></xsl:value-of>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:when test="red">
<xsl:element name="p">
<xsl:value-of select="red"></xsl:value-of>
</xsl:element>
</xsl:when>
<xsl:when test="italic">
<xsl:element name="p">
<xsl:element name="i">
<xsl:value-of select="italic"></xsl:value-of>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="p">
<xsl:apply-templates/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<p><b>Hello, world.</b></p>
<p><i>fine.</i></p>
<p style="color:red;">I am </p>
<p><b>Hello, world.</b></p>
xsl:choose
you could do something like:<xsl:template match="source">
<div>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="source/*">
<xsl:choose>
<xsl:when test="self::bold">
<p><b><xsl:apply-templates/></b></p>
</xsl:when>
<xsl:when test="self::italic">
<p><i><xsl:apply-templates/></i></p>
</xsl:when>
<xsl:when test="self::red">
<p style="color:red;"><xsl:apply-templates/></p>
</xsl:when>
<xsl:otherwise>
<p><xsl:apply-templates/></p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
xsl:choose
matches the actual elements children of source
- unlike your attempt that took place at the parent source
level. self
axis) and itself only. Unlike your attempt, that tested the children of source
and returned true for the first test if any child of source
was <bold>
.