簡體   English   中英

XSL-將值附加到XML節點(HTML)中的現有屬性

[英]XSL - append value to existing attribute in XML node (HTML)

我想將值附加到與xsl-template匹配的給定節點中的某些屬性

這是我的代碼。 有人可以告訴我為什么不工作嗎? :)

    <xsl:template match="//*[contains(@class,'right-aligned') and @style]">
       <xsl:variable name="currentAttributeValue" select="@style"/>
       <xsl:attribute name="style">
          <xsl:value-of select="concat($currentAttributeValue, 'text-align: right !important;')"  />
       </xsl:attribute>
    </xsl:template>

我也嘗試過使用參數調用“幫助程序”模板:

    <xsl:template match="//*[contains(@class,'full-width') and @style]">
       <xsl:variable name="currentAttributeValue" select="@style"/>
       <xsl:call-template name="concat">
          <xsl:with-param name="first" select="$currentAttributeValue"/>
          <xsl:with-param name="second">width: 100% !important; display: inline-block;</xsl:with-param>
       </xsl:call-template>
    </xsl:template>

這是幫手:

    <xsl:template name="concat">
       <xsl:param name="first" />
       <xsl:param name="second" />
       <xsl:attribute name="style">
          <xsl:value-of select="$first" />
           <xsl:value-of select="$second" />
       </xsl:attribute>
    </xsl:template>

但這也不起作用...有什么建議嗎?

我建議為屬性寫一個模板,例如http://xsltransform.net/jxDigUy

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[contains(@class,'full-width')]/@style">
        <xsl:attribute name="style" select="concat(., 'text-align: right !important;')"/>
    </xsl:template>
</xsl:transform>

在XSLT 1.0中,您沒有xsl:attributeselect xsl:attribute ,因此需要將該模板實現為

    <xsl:template match="*[contains(@class,'full-width')]/@style">
        <xsl:attribute name="style">
          <xsl:value-of select="concat(., 'text-align: right !important;')"/>
        </xsl:attribute>
    </xsl:template>

從這里: https//www.sitepoint.com/community/t/xslt-appending-to-a-attribute/1669

建議是:

<xsl:attribute name="attr">

    <!-- copy the current value of attr -->
    <xsl:value-of select="@attr" />

    <!-- add more to attr here -->
    blahblah

</xsl:attribute>

有關@的更多信息: https : //www.w3schools.com/xml/xpath_syntax.asp

@選擇屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM