【问题标题】:embedding tags in xslt在 xslt 中嵌入标签
【发布时间】:2011-06-08 05:42:02
【问题描述】:

我有这个正在工作的 xslt:

<xsl:when test="area_of_expertise">

    <div>
        <xsl:value-of select="area_of_expertise"/>
    </div>
</xsl:when>

但我需要的是:

<xsl:when test="area_of_expertise">

    <div id="<xsl:value-of select="area_of_expertise"/>">
        <xsl:value-of select="area_of_expertise"/>
    </div>
</xsl:when>

但是第二个例子有错误..有人知道为什么吗?

顺便说一句,有没有办法我们可以将节点的名称 area_of_expertise 转换为 areaOfExperiseLabel 并将其作为 id 插入?我真正需要的输出是这样的:

<div id="areaOfExpertiseLabel">
    asasdasdasd
</div>

【问题讨论】:

    标签: html xml xslt xpath


    【解决方案1】:

    它出错的原因是它不再是有效的 XML。

    做你想做的事:

    <xsl:when test="title">
    
        <div id="{title}">
            <xsl:value-of select="title"/>
        </div>
    </xsl:when>
    

    您可以在 {} 标记内放置任何类型的选择器,如果您有复杂的东西,甚至可以引用变量。

    <xsl:variable name="some_complex_variable">
        <xsl:value-of select="title"/>
    </xsl:variable>
    
    <xsl:when test="title">
    
        <div id="{$some_complex_variable}">
            <xsl:value-of select="title"/>
        </div>
    </xsl:when>
    

    第三种冗长的方法是使用 xsl:attribute: 动态附加属性:

    <xsl:when test="title">
    
        <div>
            <xsl:attribute name="id" select="title"/>
        </div>
    </xsl:when>
    

    【讨论】:

    • 嘿,谢谢你的帮助。顺便说一句,我已经更新了问题,你能帮我做第二部分吗?
    • name().) 或 local-name(.) 应该可以在您想要的任何地方获得节点的名称。例如,如果您处于循环中,
      将起作用。这 ”。”只是一个选择器。您可以将其替换为任何 XPath。
    【解决方案2】:

    对于第二部分,请尝试使用此模板:

    <xsl:template name="parse">
            <xsl:param name="input"/>
            <xsl:param name="position"/>
    
    
            <xsl:if test="$position &lt;= string-length($input)">
    
                <xsl:choose>
                    <xsl:when test="substring($input, $position, 1) = '_'">
                        <xsl:value-of select="translate(substring($input, $position + 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
    
                        <xsl:call-template name="parse">
                            <xsl:with-param name="input" select="$input"/>
                            <xsl:with-param name="position" select="$position + 2"/>
                        </xsl:call-template>
                    </xsl:when>
    
                    <xsl:otherwise>
    
                        <xsl:value-of select="substring($input, $position, 1)"/>
    
                        <xsl:call-template name="parse">
                            <xsl:with-param name="input" select="$input"/>
                            <xsl:with-param name="position" select="$position + 1"/>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
    
            </xsl:if>
    
    
        </xsl:template>
    

    用法:

    <xsl:call-template name="parse">
                <xsl:with-param name="input" select="'area_of_expertise'"/>
                <xsl:with-param name="position" select="1"/>
            </xsl:call-template>
    

    【讨论】:

    • 看在上帝的份上,请使用库而不是像这样手动解析。请参阅 DCharness 的回答。
    • @Jordan 嘿,我无法得到 dcharness 的工作答案:stackoverflow.com/questions/6289509/… 你能帮我吗?谢谢
    • @Pacerier 你真的应该打开另一个问题。您接受的答案与您提出的原始问题无关。
    • @Pacerier,是的,我明白这一点,但您编辑了原始问题以包含第二部分,它通常应该是一个单独的问题,而不是添加一些东西,否则答案会变得非常复杂。
    【解决方案3】:

    对于第二部分,从下划线转换为驼峰式,您可能需要查看String Processing in the XSLT Standard Librarystr:subst() 下划线分割,str:to-camelcase() 适当改变字母大小写,concat() 添加“标签”后缀,你应该设置。

    【讨论】:

    • 嘿,你能帮我解决这个问题吗?在那个页面上写着 str:substr() disable-output-escaping: A value of yes indicates that the result should have output escaping disabled. Any other value allows normal escaping of text values. The default is to enable output escaping. 但我还是不明白它在说什么..
    • 嘿,我无法让它工作:stackoverflow.com/questions/6289509/…
    • 通常,XSLT 输出会在文本节点输出中转义 XML 特有的字符“& 和 w3.org/TR/xslt#disable-output-escaping
    【解决方案4】:

    对于第一部分,您可以使用:

    <xsl:when test="area_of_expertise">
        <div>
            <xsl:attribute name="id">
                <xsl:value-of select="area_of_expertise"/>
            </xsl:attribute>
    
            <xsl:value-of select="area_of_expertise"/>
        </div>
    </xsl:when>
    

    【讨论】:

      【解决方案5】:

      这可能是一个愚蠢的答案,但您似乎需要这个简单的跟踪:

      <xsl:when test="area_of_expertise">
          <div id="areaOfExperiseLabel">
              <xsl:value-of select="area_of_expertise"/>
          </div>
      </xsl:when>
      

      否则,如果您需要另一个字符串,为什么您对&lt;xsl:value-of select="area_of_expertise"/&gt;@id 感兴趣?

      【讨论】:

      • 我不想自己输入areaOfExperiseLabel,我想有一种方法让计算机从area_of_expertise 中导出它
      • 但是,您的要求。如果您不知道元素名称 a priori,则这是有道理的。那是你的情况吗?而在那种情况下,你怎么能匹配呢?
      • 我需要一个函数来做到这一点,因为area_of_expertise 不是唯一需要这种转换的“字符串”,还有其他 10 个需要这种驼峰转换
      • 好的,那么其他答案有什么不好的呢?你搜索 XSLT 1.0 还是 2.0?
      • @empo 我无法得到 dcharness 的工作答案:stackoverflow.com/questions/6289509/…
      猜你喜欢
      • 2015-04-16
      • 2013-06-17
      • 1970-01-01
      • 2015-06-26
      • 2012-03-23
      • 2011-04-18
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      相关资源
      最近更新 更多