[英]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.