2012-01-13 50 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