簡體   English   中英

XSL 轉換: select XPath 屬性值基於當前節點屬性值

[英]XSL Transformation: select XPath property value based on current nodes property value

將 xml 轉換為 html,我想根據存儲在 xml 文檔中其他位置的信息為元素分配類名。 為此,我需要將當前節點的屬性值插入 XPath。 我不知道該怎么做。

鑒於此 xml:

    <?xml version="1.0"?>
    <Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
     <Styles>
      <Style ss:ID="cell-ID" ss:Name="classname">
      </Style>
     </Styles>
     <Worksheet>
        <Cell ss:StyleID="cell-ID">test</Cell>
       </Worksheet>
    </Workbook>

我使用了以下 XSL,但是帶有箭頭指針的行不起作用:

<?xml version="1.0"?>
    
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
                    exclude-result-prefixes="ss"
                    version="1.0">
    <xsl:output method="html"/>
    
    <xsl:template match="/">
        <xsl:element name="style">
            <xsl:for-each select="Workbook/Styles/Style">
                #<xsl:value-of select="@ss:ID"/>
                    <xsl:if  test="@ss:Name">, .<xsl:value-of select="@ss:Name" /></xsl:if> { ... }
            </xsl:for-each>
        </xsl:element>
        <xsl:for-each select="Workbook/Worksheet/Cell">
            <xsl:element name="div">
                <xsl:attribute name="class">default</xsl:attribute>
                <xsl:if test="@ss:StyleID">
                    <xsl:attribute name="id">
                        <xsl:value-of select="@ss:StyleID"/>
                    </xsl:attribute>
                    <xsl:attribute name="class">
==>                     <xsl:value-of select="//Style[@ss:ID=@ss:StyleID]/@ss:Name"/>
                    </xsl:attribute>
                </xsl:if>
                <xsl:value-of select="."/>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

所需的 output 將是

<style>
   #cell-ID, .classname { ... }
</style>
<div class="classname" id="cell-ID">test</div>

但類名仍然為空。

非常感謝任何幫助。

XSLT 具有用於解決交叉引用的內置密鑰機制。

考慮這個最小的例子:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
exclude-result-prefixes="ss">

<xsl:key name="styl" match="Style" use="@ss:ID" />

<xsl:template match="/Workbook">
    <html>
        <body>
            <xsl:for-each select="Worksheet/Cell">
                <div class="{key('styl', @ss:StyleID)/@ss:Name}">
                    <xsl:value-of select="."/>
                </div>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

應用於您問題中的輸入,這將產生:

結果

<html>
   <body>
      <div class="classname">test</div>
   </body>
</html>

請注意 (1) 文字結果元素和 (2) 屬性值模板的使用。

暫無
暫無

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

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