xml 使用 xsl 变量捕获为我返回空白的调用模板的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6336794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using xsl variable to capture output of call-template returning blank for me
提问by testing123
I have seen lots of posts that do something like this and that makes me feel like this is possible and I am just doing something wrong. I have simplified it as much as possible to try and figure out why this is happening:
我看到很多帖子都做这样的事情,这让我觉得这是可能的,我只是做错了什么。我已经尽可能地简化它以试图弄清楚为什么会发生这种情况:
Heres my xml (nothing very exciting):
这是我的 xml(没什么特别令人兴奋的):
<?xml version="1.0" encoding="UTF-8"?>
<REPORT>
</REPORT>
Here is my xsl:
这是我的 xsl:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="REPORT">
<xsl:variable name="tryThisTemplate">
<xsl:call-template name="TRY_THIS"/>
</xsl:variable>
<TEST1>
<xsl:call-template name="TRY_THIS"/>
</TEST1>
<TEST2>
<xsl:value-of disable-output-escaping="yes" select="$tryThisTemplate" />
</TEST2>
<TEST3>
<xsl:value-of select="$tryThisTemplate" />
</TEST3>
</xsl:template>
<xsl:template name="TRY_THIS">
<MY_NODE desc="my description" />
</xsl:template>
</xsl:stylesheet>
Here is my result:
这是我的结果:
<?xml version="1.0" encoding="utf-8"?>
<TEST1>
<MY_NODE desc="my description"/>
</TEST1>
<TEST2></TEST2>
<TEST3></TEST3>
Here is my question: How come TEST2 and TEST3 don't work. The $tryThisTemplate variable appears to be blank. Am I misunderstanding something here. Should I be doing this in a different way?
这是我的问题:为什么 TEST2 和 TEST3 不起作用。$tryThisTemplate 变量似乎为空。我在这里误解了什么。我应该以不同的方式做这件事吗?
回答by Dimitre Novatchev
Here is the correct way to do this(note that DOE is not necessary and should be avoided):
这是执行此操作的正确方法(请注意,DOE 不是必需的,应该避免):
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="REPORT">
<xsl:variable name="tryThisTemplate">
<xsl:call-template name="TRY_THIS"/>
</xsl:variable>
<TEST1>
<xsl:call-template name="TRY_THIS"/>
</TEST1>
<TEST2>
<xsl:copy-of select="$tryThisTemplate" />
</TEST2>
<TEST3>
<xsl:copy-of select="$tryThisTemplate" />
</TEST3>
</xsl:template>
<xsl:template name="TRY_THIS">
<MY_NODE desc="my description" />
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
当此转换应用于提供的 XML 文档时:
<REPORT>
</REPORT>
the wanted result is produced:
产生了想要的结果:
<TEST1>
<MY_NODE desc="my description"/>
</TEST1>
<TEST2>
<MY_NODE desc="my description"/>
</TEST2>
<TEST3>
<MY_NODE desc="my description"/>
</TEST3>
Explanation:<xsl:copy-of>copies (as its name says) nodes. <xsl:value-of>outputs the string value of whatever is in its selectattribute. The string value of an element is the concatenation (in document order) of all of its text-node descendents. In your case the element has no text-node descendents and thus <xsl:value-of>outputs nothing.
解释:<xsl:copy-of>复制(顾名思义)节点。<xsl:value-of>输出其select属性中任何内容的字符串值。元素的字符串值是其所有文本节点后代的串联(按文档顺序)。在您的情况下,该元素没有文本节点后代,因此不<xsl:value-of>输出任何内容。
回答by LarsH
Yes there is a misunderstanding here. If you are trying to copy the structure of $tryThisTemplateto the output, you need to use <xsl:copy-of>instead of <xsl:value-of>. <xsl:value-of>outputs the string value of its select argument, that is, its text content, which in this case is an empty string.
是的,这里有一个误解。如果您尝试将 的结构复制$tryThisTemplate到输出,则需要使用<xsl:copy-of>代替<xsl:value-of>。<xsl:value-of>输出其 select 参数的字符串值,即其文本内容,在本例中为空字符串。
回答by Emiliano Poggi
The $tryThisTemplate variable appears to be blank
$tryThisTemplate 变量似乎为空
The variable it's not blank, but with xsl:value-ofyou are asking for the text nodes inside that. That's "blank".
变量不是空白,但xsl:value-of您要求其中的文本节点。那就是“空白”。
For instance, try with:
例如,尝试使用:
<TEST3>
<xsl:copy-of select="$tryThisTemplate" />
</TEST3>
And you'll see magically appear MY_NODEbetween TEST3:))
你会看到神奇地出现MY_NODE在TEST3:))
回答by Basheer AL-MOMANI
check out my solution
查看我的解决方案
here is my template (was veriable content)
这是我的模板(是真实的内容)
<xsl:template name="author-name" match="string-name">
<xsl:if test="fn[string-length(normalize-space()) > 0] or given-names[string-length(normalize-space()) > 0]">
<xsl:apply-templates select="fn | given-names" mode="ascii"/>
</xsl:if>
<xsl:if test="sn[string-length(normalize-space()) > 0] or surname[string-length(normalize-space()) > 0]">
<xsl:text> </xsl:text><xsl:apply-templates select="sn | surname" mode="ascii"/>
</xsl:if>
<xsl:if test="sn[string-length(normalize-space()) > 0] or suffix[string-length(normalize-space()) > 0]">
<xsl:text> </xsl:text><xsl:apply-templates select="sn | suffix" mode="ascii"/>
</xsl:if>
</xsl:template>
and when I use it in any other template simply I just call it like this
当我在任何其他模板中使用它时,我只是这样称呼它
<xsl:template match="string-name">
<xsl:variable name="author">
<xsl:call-template name="author-name"/> <!--here is the tricky part-->
</xsl:variable>
<span class="NLM_{name()}">
<xsl:copy-of select="@id" />
<xsl:value-of select="normalize-space($author)" />
</span>
</xsl:template>
hope this helps you
希望这对你有帮助

