gpt4 book ai didi

xslt - 使用xslt变量在html标记中输出属性名称和属性值

转载 作者:行者123 更新时间:2023-12-03 17:06:28 25 4
gpt4 key购买 nike

我必须将微数据(schema.org)放在html标签中,如下所示:

<div class="list_item" itemtype="http://schema.org/Hotel" itemscope="itemscope">


我想将所有微数据内容放入一个这样的变量中:

<xsl:param name="schema_main">
<xsl:choose>

<xsl:when test="$list_type = 'hotels'">
<xsl:text>itemtype="http://schema.org/Hotel" itemscope="itemscope"</xsl:text>
</xsl:when>

<xsl:when test="$list_type = 'sight'">
<xsl:text>itemtype="http://schema.org/CivicStructure" itemscope="itemscope"</xsl:text>
</xsl:when>

</xsl:choose>
</xsl:param>


我不能这样做,因为我必须在xslt中指定属性名称以放置任何内容:

 <div class="list_item" itmescope="{$some_variable}">


所以问题是-我可以将带有属性名称和属性值的字符串放在xslt的html标记中吗?就像是:

<div class="list_item" $variable_with_attribute_name_and_attribute_value>


提前致谢

最佳答案

我认为您无法以描述的方式使用参数。但是,如果目标是删除重复的编码,则可以通过命名模板而不是变量来实现。

例如,这是一个命名模板

   <xsl:template name="schema_main">
<xsl:attribute name="itemtype">
<xsl:choose>
<xsl:when test="$list_type = 'hotels'">
<xsl:text>http://schema.org/Hotel</xsl:text>
</xsl:when>
<xsl:when test="$list_type = 'sight'">
<xsl:text>http://schema.org/CivicStructure</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="itemscope">itemscope</xsl:attribute>
</xsl:template>


并调用它,将属性添加到当前元素,只需执行此操作

  <div>
<xsl:call-template name="schema_main"/>
</div>


然后输出如下:

<div itemtype="http://schema.org/Hotel" itemscope="itemscope" />


假设您已将list_type设置为酒店。注意,如果list_type在范围上不是全局的,则可以将参数传递给命名模板。

关于xslt - 使用xslt变量在html标记中输出属性名称和属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com