简体   繁体   English

xslt中未关闭的html标记

[英]unclosed html tag inside xslt

I'd like to have unclosed html tag as a result of xslt. 由于xslt,我想要使用未关闭的html标记。 I'll add closing tag later in xslt. 我稍后会在xslt中添加结束标记。 How can I achieve this? 我怎样才能做到这一点? This one doesn't compile: 这个不编译:

<xsl:when test="$href">
     <xsl:text><a href='{$href}'></xsl:text>
 </xsl:when>

Thanx 感谢名单

This is the kind of thing that you probably should want to avoid at all costs. 这是你可能想要不惜一切代价避免的事情。 I do not know your requirements but you perhaps want a link or a span tag based on something. 我不知道您的要求,但您可能需要基于某些内容的链接或跨度标记。

In these instances you can use something like this 在这些情况下,您可以使用这样的东西

<xsl:apply-templates select="tag"/>

then 2 templates ie 那么2个模板即

<xsl:template match="tag">
    <span>hello king dave</span>    
</xsl:template>

<xsl:template match="tag[@href]">
    <a href="{@href}">link text....</a>    
</xsl:template>

It's hard to give a definite answer without a better idea of the precise use case, but it's worth noting that you can use match and name on the same <xsl:template> . 如果没有更好地了解确切的用例,很难给出明确的答案,但值得注意的是,您可以在相同的<xsl:template>上使用match name For example, if you want to produce some particular output for all <tag> elements, but also wrap this output in an <a> tag in certain cases, then you could use an idiom like 例如,如果要为所有<tag>元素生成一些特定输出,但在某些情况下也将此输出包装在<a>标记中,那么您可以使用类似的习惯用法

<xsl:template match="tag[@href]">
  <a href="{@href}"><xsl:call-template name="tagbody" /></a>
</xsl:template>

<xsl:template match="tag" name="tagbody">
  Tag content was "<xsl:value-of select="."/>"
</xsl:template>

The idea here is that tag elements with an href will match the first template, which does some additional processing before and after calling the general tag template. 这里的想法是带有href tag元素将匹配第一个模板,该模板在调用通用tag模板之前和之后进行一些额外的处理。 Tags without an href will just hit the normal template without the wrapping logic. 没有href标签只会在没有包装逻辑的情况下点击普通模板。 Ie for an input like 即输入像

<root>
  <tag>foo</tag>
  <tag href="#">bar</tag>
</root>

you would get an output like 你会得到像这样的输出

Tag content was "foo"
<a href="#">Tag content was "bar"</a>

I had the same problem before and was only able to solve it by copying the entire <a href='{$href}'>...</a> for each when branch. 我以前遇到过同样的问题,只能通过复制每个分支when的整个<a href='{$href}'>...</a>来解决它。

Maybe you could try setting the doctype of your XSL to some loose XML standard, but afaik XSLT is pretty strict. 也许您可以尝试将XSL的doctype设置为一些松散的XML标准,但是XSLT非常严格。

Edit: apparently you can set the doctype with a <xsl:output> tag. 编辑:显然您可以使用<xsl:output>标记设置doctype。

Found solution on the net: 在网上找到解决方案:

        <xsl:text disable-output-escaping="yes"><![CDATA[<a href=']]></xsl:text>
                 <xsl:value-of select="href"/>
        <xsl:text disable-output-escaping="yes"><![CDATA['>]]></xsl:text>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM