English 中文(简体)
xslt 1.0 rewrite Value of a node
原标题:xslt 1.0 rewrite values of a node according to a map
  • 时间:2012-01-13 15:30:49
  •  标签:
  • xslt

在修改一份文件时,我需要研究地图中的某些节点内容,并撰写这些价值观。

我在转变中勾画了我的地图。

<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>

I m always getting ERROR value. I can t put map value s into attributes, because they contain letters that get escaped.

我怎么能做到这一点?

最佳回答

我认为这里有几个问题:

  • You seem to be looking for key elements in your variable, but they re called kat there (typo?)
  • You seem to be trying to reference the bar child of the context node inside the loop, but you need to use current() to do that
  • You should create this map as elements in your own namespace instead of an 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>

a. 产生这一产出(一比):

stuff
ERROR
ERROR
ERROR
问题回答

暂无回答




相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签