2012-01-13 32 views
0

在轉換文檔時,我需要在'地圖'中查找某些節點內容,然後寫入這些值。xslt 1.0根據地圖重寫節點的值

我在轉換中嵌入了我的'映射'。

<xsl:variable name="inlinedmap"> 
    <kat id="stuff">value</kat> 
    <!-- ... --> 
</xsl:variable> 
<xsl:variable name="map" select="document('')/xsl:stylesheet/xsl:variable[@name='inlinedmap']" /> 
<xsl:template match="/"> 
    <xsl:for-each select="/*/foo"> 
     <!-- 'bar' contents should equal to contents of 'kat' --> 
     <xsl:variable name="g" select="$map/key[.=bar]"/> 
     <xsl:choose> 
      <xsl:when test="$g != ''"> 
       <xsl:value-of select="$g/@id"/> 
      </xsl:when> 
      <xsl:otherwise> 
       ERROR 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:for-each> 
</xsl:template> 

我總是收到錯誤值。 我無法將地圖值放入屬性中,因爲它們包含會被轉義的字母。

我該如何讓它工作?

回答

1

我覺得這裏有幾個問題:

  • 你似乎在尋找您的變量key元素,但他們是所謂的kat
  • 你似乎想要(錯字?)引用bar子循環內的上下文節點的,但你需要使用current()
  • 您應該創建此地圖作爲自己的名稱空間的元素,而不是一個xsl:variable

下面是一個完整的例子。這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:my="my"> 
    <my:vars> 
     <kat id="stuff">value</kat> 
     <!-- ... --> 
    </my:vars> 
    <xsl:variable name="map" select="document('')/*/my:vars/*"/> 
    <xsl:template match="/"> 
     <xsl:for-each select="/*/foo"> 
      <!-- 'bar' contents should equal to contents of 'kat' --> 
      <xsl:variable name="g" select="$map[.=current()/bar]"/> 
      <xsl:choose> 
       <xsl:when test="$g != ''"> 
        <xsl:value-of select="$g/@id"/> 
       </xsl:when> 
       <xsl:otherwise> 
        ERROR 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

應用於此輸入:

<root> 
    <foo><bar>value</bar></foo> 
    <foo><bar>value1</bar></foo> 
    <foo><bar>value2</bar></foo> 
    <foo><bar>value3</bar></foo> 
</root> 

產生這樣的輸出(一個匹配):

stuff 
ERROR 
ERROR 
ERROR 
+0

試了一下,什麼都沒有改變 – 2012-01-13 16:49:40

+1

@ŁukaszGruner:這是知識性「看着窗外,它仍在下雨」。您尚未提供源XML文檔。你也沒有提供完整的轉換。更不用說通緝的結果了......如果人們無法複製你的問題,那可能只是你想象中的產物!請編輯並改進您的問題。另外,請多加尊重少數勇敢的人,他們可能會敢於猜測你的意思。 – 2012-01-14 04:11:44

+0

正常工作,主要問題(答案中解釋的東西除外)是我的地圖編碼 – 2012-01-18 11:14:54