How to dynamically assign name and href for anchor

2019-05-31 13:30发布

I have a requirement where the xml might have one or more services (name might be different), and I am having a content which should have all of these available services from the xml something like below

<li><a href="#_cms">CMS</a></li>
<li><a href="#_dis">DIS</a></li>

but above I have hardcoded the a tag content and href since I know these are the values, but in real time I would not be knowing these names, so how to set href and anchor tag contents based on xml values?

So far I got the below for-each statement, which gets me all the service names from the xml

<xsl:variable name="number">
  <xsl:number/>
</xsl:variable>

<xsl:for-each select="csmclient/product/domainmetadata/domainmetadata_service">
    <li><a href="#_ser{$number}"><xsl:value-of select="@name"/><xsl:value-of select="position()"/></a></li>
</xsl:for-each>

.
.
.

<!--far below end-->
<xsl:for-each select="domainmetadata/domainmetadata_service">
    <h3>Service Name: <span style="color:#328aa4"><a name="_ser{$number}" href="#_top"><xsl:value-of select="@name"/></a></span></h3>
    .
    .
    .
</xsl:for-each>

but it does not seem to work, it gives me all my services but the link does not work. Any other ideas?

Note: I took help from this question link which had a similar requirement.

标签: xslt
3条回答
Explosion°爆炸
2楼-- · 2019-05-31 13:55

Use something like below, should work

<xsl:for-each select="csmclient/product/domainmetadata/domainmetadata_service">
    <li><a href="#{generate-id()}"><xsl:value-of select="@name"/></a></li>
</xsl:for-each>

<xsl:for-each select="domainmetadata/domainmetadata_service">
    <h3>Service Name: <span style="color:#328aa4"><a name="{generate-id()}" href="#_top"><xsl:value-of select="@name"/></a></span></h3>
</xsl:for-each> 
查看更多
再贱就再见
3楼-- · 2019-05-31 14:01
 <li><a href="#_ser{$number}"><xsl:value-of select="@name"/><xsl:value-of select="position()"/></a></li>

but it does not seem to work ...

What you are trying to write is this (not discussing at all if this uniquely identifies the node):

<li>
  <a href="#_ser{$number}{@name}{position()}"/>
</li> 
查看更多
成全新的幸福
4楼-- · 2019-05-31 14:11

There is a xslt function generate-id() which gives and unique textual identifier for any node in the xml.

http://www.w3.org/TR/xslt#function-generate-id

查看更多
登录 后发表回答