2011-04-06 121 views
1

我想根据xslt的xml文件中的字段生成html输出。我根据XMLXSLT换行符问题

祖父母父母与子女,孙子女关系。例如他们的名字:

<root> 
    <node1> 
     <node2> 
     <node3>Data</node3> 
     </node2> 
    </node1> 

我需要的是创造什么让我们说文本框,用名称node1__node2__node3 我到目前为止是这样的

<input type="text" name="node1__ 
     node2__ 
     node3__" 

但我想要的是:

<input type="text" name="node1__node2__node3__"/> 

所以它没用。我的XSLT来产生这种无用的输出为:

<xsl:template name="chooseNameID"> 
    <xsl:param name="currentNode"/><!-- in this case currentNode is node3 --> 
    <xsl:variable name="fieldNames"> 
     <xsl:for-each select="$currentNode/ancestor::*"> 
       <xsl:value-of select="name(.)"/>__ 
     </xsl:for-each> 
    </xsl:variable> 

    <xsl:attribute name="name"> 
     <xsl:value-of select="$fieldNames"/>        
    </xsl:attribute> 

</xsl:template> 

我想这个问题是在<xsl:value-of但我无法找到任何解决办法了这一点。

感谢

+0

好问题,+1。查看我的答案,获得完整,简短和简单的解决方案。 :) – 2011-04-06 13:46:22

回答

1

这种转变

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node3"> 
    <xsl:variable name="vName"> 
    <xsl:for-each select= 
     "ancestor-or-self::*[not(position()=last())]"> 
     <xsl:value-of select="name()"/> 
     <xsl:if test="not(position()=last())">__</xsl:if> 
    </xsl:for-each> 
    </xsl:variable> 

    <input type="text" name="{$vName}"/> 
</xsl:template> 
</xsl:stylesheet> 

时所提供的XML文档应用:

<root> 
    <node1> 
     <node2> 
      <node3>Data</node3> 
     </node2> 
    </node1> 
</root> 

产生想要的,正确的结果:

<input type="text" name="node1__node2__node3"/> 

请注意:使用AVT(属性值模板)在一条短线中生成所需的输出。

0

为正常,问一个问题后,你找到一个解决方案。

更改<xsl:value-of select="$fieldNames"/><xsl:value-of select="normalize-space($fieldNames)"为我工作。

+1

这将仍然在结果中留下空格,因为normalize-space()将用一个空格替换每个内部空白序列的空格。 – 2011-04-06 14:45:28

+0

你说得对。我只是错过了空间。 – savruk 2011-04-06 14:49:47

1

不需要的空白,包括换行符,是循环中文本节点文字的一部分。

在样式表文档中,除了在xsl:text之内,只有空白的文本节点被忽略。但是,与其他文本相邻的空格是该文本的一部分。

样式表中的文字空白可以使用xsl:text进行管理。

<!-- change this --> 
    <xsl:for-each select="$currentNode/ancestor::*"> 
     <xsl:value-of select="name(.)"/>__ 
    </xsl:for-each> 

    <!-- to this --> 
    <xsl:for-each select="$currentNode/ancestor::*"> 
     <xsl:value-of select="name(.)"/>__<xsl:text/> 
    </xsl:for-each> 

    <!-- or this --> 
    <xsl:for-each select="$currentNode/ancestor::*"> 
     <xsl:value-of select="name(.)"/> 
     <xsl:text>__</xsl:text> 
    </xsl:for-each> 
+0

+1正确的解释。 – 2011-04-06 17:28:36