XSLT换行问题

时间:2011-04-06 13:18:23

标签: xml xslt

我正在尝试使用xslt根据xml文件中的字段生成html输出。我根据祖父母 - 父 - 子 - 孙子关系在xml中命名它们

例如:

<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,但我找不到任何解决办法。

由于

3 个答案:

答案 0 :(得分: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(属性值模板)在一条短线中生成所需的输出。

答案 1 :(得分: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>

答案 2 :(得分:0)

正常情况下,在提出问题后,您会找到解决方案。

使用此<xsl:value-of select="$fieldNames"/>更改<xsl:value-of select="normalize-space($fieldNames)"行对我有用。

相关问题