简体   繁体   English

XSLT不能使用xsl:attribute设置href属性

[英]XSLT can't use xsl:attribute to set href attribute

I'm having a strange problem and I am also only commencing to get my head around XSLT for a project. 我遇到了一个奇怪的问题,我也只是开始着手研究XSLT。 I defined a variable called collapeId which is successfully getting the value 11. I then use this variable to try and set attributes href, data-target in the <a> element and id in the <div> element. 我定义了一个名为collapeId的变量,该变量成功获取了值11。然后,我使用该变量尝试设置属性href, <a>元素中的data-target和<div>元素中的id The problem I'm having is that I get href="%0A%09%09%09%09#11" in my output for the href attribute but it sets the other attributes fine. 我遇到的问题是我在href属性中的输出中得到了href="%0A%09%09%09%09#11" ,但它可以将其他属性设置得很好。

Any ideas why the same code is behaving differently with href and how I can fix this? 有什么想法为什么相同的代码与href的行为不同以及如何解决此问题?

Given the following XSLT I've written. 鉴于我已经编写了以下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>

I get the following output. 我得到以下输出。

<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>

Thanks all! 谢谢大家!

You are creating the attribute like so 您正在像这样创建属性

<xsl:attribute name="href">
    #<xsl:value-of select="$collapseId" />
</xsl:attribute>  

But you have indented the text node containing # . 但是您已经缩进了包含#的文本节点。 This means it is not just adding # to the attribute, but the new line, and spaces, before it. 这意味着它不仅在属性上添加了# ,而且还在其前添加了新行和空格。

Change it to this 改成这个

<xsl:attribute name="href">
    <xsl:text>#</xsl:text>
    <xsl:value-of select="$collapseId" />
</xsl:attribute>  

The difference here is that XSLT will ignore "whitespace only" text nodes, so in this case the newline and spaces won't get output. 区别在于XSLT将忽略“仅空白”文本节点,因此在这种情况下,换行符和空格将不会输出。

Note, if you could use XSLT 2.0, you could do this 请注意,如果您可以使用XSLT 2.0,则可以执行此操作

<xsl:attribute name="href" select="concat('#', $collapseId)" />

Also note, it would be more efficienet to declare the collapseId variable like so 还要注意,像这样声明collapseId变量会更有效

<xsl:variable name="collapseId" select="sectiontitle/@pardef"/>

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

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