用XSL生成xml属性值

用XSL生成xml属性值,xml,xslt,Xml,Xslt,我不熟悉使用XSL。我正在尝试将输入的数据转换为datacapture模板并生成xml文件。我捕获的数据应该是属性值。我知道我的路径是正确的,但我在生成过程中出错。救命啊 <!-- <xsl:value-of select='odnodes/node/comments'/> --> <node name="<xsl:value-of select='odnodes/node/name'/>" host="<xsl:value-of s

我不熟悉使用XSL。我正在尝试将输入的数据转换为datacapture模板并生成xml文件。我捕获的数据应该是属性值。我知道我的路径是正确的,但我在生成过程中出错。救命啊

<!-- <xsl:value-of select='odnodes/node/comments'/> -->
<node name="<xsl:value-of select='odnodes/node/name'/>" 
      host="<xsl:value-of select='odnodes/node/host'/>" 
      port="<xsl:value-of select='odnodes/node/port'/>" 
/>

最终结果如下所示:

<!-- uat server - added 2/7/2013 -->
<node name="webserver_uat" host="192.168.1.1" port="20014" />

属性值模板是您的朋友。您试图实现的正确语法是

<node name="{odnodes/node/name}" 
      host="{odnodes/node/host}" 
      port="{odnodes/node/port}" />

这里的大括号表示它是一个要计算的表达式,而不是输出

注意,您还可以使用xsl:attribute来创建属性:

<node>
   <xsl:attribute name="name"><xsl:value-of select="odnodes/node/name" /></xsl:attribute>
   <xsl:attribute name="host"><xsl:value-of select="odnodes/node/name" /></xsl:attribute>
   <xsl:attribute name="port"><xsl:value-of select="odnodes/node/name" /></xsl:attribute>
</node>


但正如您所看到的,这更为详细,只有在需要“条件”属性时才真正需要这样做。(例如,您可以将其中一个属性包装在xsl:if中,或者根据输入XML中的值更改属性名称)。

感谢您的快速响应!我使用了xls:attribute解决方案,并为select使用了相对路径,这就成功了。非常感谢。