簡體   English   中英

XSLT替換ID,獲取其他節點的值

[英]XSLT Replace Id, Get Value of other node

大家好,我是xml / xsl的新手。 我有以下xml代碼

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Miau.xsl"?>
<export xmlns="urn:VocabularyExport">

<categories>
    <category name="A" id="123" />
    <category name="B" id="456" />
</categories>

<vocables>
    <vocable categoryId="123" spanishWord="uno" engWord="one" id="45d0a344-785b-
4463-9877-5474c99fdc74" />
    <vocable categoryId="456" spanishWord="dos" engWord="two" id="03efffbb-1cbf-
44f9-a377-74da252daac0" />
</vocables>
</export>

為了在一個表中顯示它,我有以下(有效的)XSL代碼:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:l="urn:VocabularyExport">

<xsl:key name="category" match="category" use="@id" />
<xsl:template match="/">
<html>
   <body>

    <h1>My Vocable Colletion</h1>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Id</th>
        <th>Deutsches Wort</th>
        <th>Spanishes Wort</th>
        <th>CategoryId</th>
      </tr>
      <xsl:for-each select="l:export/l:vocables/l:vocable">
        <tr>
          <td>
            <xsl:value-of select="@id"/>
          </td>
          <td>
            <xsl:value-of select="@germanWord"/>
          </td>
          <td>
            <xsl:value-of select="@spanishWord"/>
          </td>
          <td>
            <xsl:value-of select="@categoryId"/>
          </td>
        </tr>
       </xsl:for-each>
      </table>
     </body>
    </html>
  </xsl:template>
 </xsl:stylesheet>

取而代之的是中categoryId我想和類別名稱來取代它(見節點categories )。 有什么建議嗎?

對於XML / XSLT的新手來說,您已經有了一個良好的開端,因為您已經在XSLT中處理名稱空間時添加了它。 但是,您已經錯過了密鑰。 應該是這個

<xsl:key name="category" match="l:category" use="@id" />

然后,要獲取類別名稱,所需的表達式是這樣的:

<xsl:value-of select="key('category', @categoryId)/@name"/>

暫無
暫無

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

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