![](/img/trans.png)
[英]How can I combine xsl:attribute and xsl:use-attribute-sets to conditionally use an attribute set?
[英]XSLT can't use xsl:attribute to set href attribute
我遇到了一个奇怪的问题,我也只是开始着手研究XSLT。 我定义了一个名为collapeId的变量,该变量成功获取了值11。然后,我使用该变量尝试设置属性href, <a>
元素中的data-target和<div>
元素中的id
。 我遇到的问题是我在href属性中的输出中得到了href="%0A%09%09%09%09#11"
,但它可以将其他属性设置得很好。
有什么想法为什么相同的代码与href的行为不同以及如何解决此问题?
鉴于我已经编写了以下XSLT。
<!-- Retrieve value of pardef attribute -->
<xsl:variable name="collapseId">
<xsl:value-of select="sectiontitle/@pardef"/>
</xsl:variable>
<!--<a class="collapsed collapsible list-group-item" style="border-width: 0px 0px 1px; border-bottom-style: solid; border-bottom-color: #dddddd; margin-bottom: 0px; color: #646464 !important;" href="#a3" rel="nofollow" data-target="#a3" data-toggle="collapse"> -->
<a class="collapsed collapsible list-group-item" style="border-width: 0px 0px 1px; border-bottom-style: solid; border-bottom-color: #dddddd; margin-bottom: 0px; color: #646464 !important;" rel="nofollow" data-toggle="collapse">
<xsl:attribute name="data-target">
#<xsl:value-of select="$collapseId"/>
</xsl:attribute>
<xsl:attribute name="href">
#<xsl:value-of select="$collapseId" />
</xsl:attribute>
<xsl:value-of select="sectiontitle"/>
</a>
<div class="collapse" style="margin: 10px 20px;">
<xsl:attribute name="id">
#<xsl:value-of select="$collapseId"/>
</xsl:attribute>
<xsl:apply-templates select="par" />
</div>
我得到以下输出。
<a class="collapsed collapsible list-group-item" style="border-width: 0px 0px 1px; border-bottom-style: solid; border-bottom-color: #dddddd; margin-bottom: 0px; color: #646464 !important;" rel="nofollow" data-toggle="collapse" data-target="
#11" href="%0A%09%09%09%09#11">Anchor Title</a>
<div class="collapse" style="margin: 10px 20px;" id="
#11">
<p>Data 1</p>
<p/>
<p>Data 2 </p>
<p/>
<p>Data 3</p>
</div>
谢谢大家!
您正在像这样创建属性
<xsl:attribute name="href">
#<xsl:value-of select="$collapseId" />
</xsl:attribute>
但是您已经缩进了包含#
的文本节点。 这意味着它不仅在属性上添加了#
,而且还在其前添加了新行和空格。
改成这个
<xsl:attribute name="href">
<xsl:text>#</xsl:text>
<xsl:value-of select="$collapseId" />
</xsl:attribute>
区别在于XSLT将忽略“仅空白”文本节点,因此在这种情况下,换行符和空格将不会输出。
请注意,如果您可以使用XSLT 2.0,则可以执行此操作
<xsl:attribute name="href" select="concat('#', $collapseId)" />
还要注意,像这样声明collapseId
变量会更有效
<xsl:variable name="collapseId" select="sectiontitle/@pardef"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.