XSLT copy-of-attribute留空

时间:2015-02-01 18:14:50

标签: xslt

我正在尝试修改现有的XSLT,以在输入XML中包含文件节点的originalfilename属性,作为输出xml中反馈的文件属性。我想我误解了副本的陈述,我非常感谢任何帮助。目前,我想要的输出在反馈节点上显示一个空文件属性。

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

  <xsl:template match="/">
    <document>
      <xsl:for-each select="files/file/segmentpair[Comments/Comment]">
        <xsl:apply-templates select="Comments/Comment" />
        <xsl:copy-of select="files|file|source|target" />
      </xsl:for-each>
    </document>
  </xsl:template>

  <xsl:template match="Comment">
    <feedback>
      <xsl:attribute name="id">
        <xsl:value-of select="count(preceding::Comments)+1" />
      </xsl:attribute>
      <xsl:attribute name="file">
        <xsl:value-of select="files/file/@originalfilename" />
      </xsl:attribute>
      <xsl:value-of select="." />
    </feedback>
  </xsl:template>
</xsl:stylesheet>

输入

<files>
  <file originalfilename="C:\Users\A\Documents\Studio 2014\Projects\15_002_\de-DE\master\advanced-materials-and-processes-msc-hons.xml">
    <segmentpair id="1" locked="False" color="245,222,179" match-value="86">
      <source>Advanced Materials and Processes (M.Sc.hons.)</source>
      <target>Advanced Materials and Processes (MSc)</target>
      <Comments>
        <Comment>[ic14epub 20.01.2015 09:28:43] 'hons' taken out (discussion of this still ongoing as far as I'm aware)</Comment>
      </Comments>
    </segmentpair>
 </file>
</files>

期望输出

    <feedback id="1" file="C:\Users\A\Documents\Studio 2014\Projects\15_002_\de-DE\master\advanced-materials-and-processes-msc-hons.xml">[ic14epub 20.01.2015 09:28:43] 'hons' taken out (discussion of this still ongoing as far as I'm aware)</feedback>
       <source>Advanced Materials and Processes (M.Sc.hons.)</source>
       <target>Advanced Materials and Processes (MSc)</target>

2 个答案:

答案 0 :(得分:1)

只需在/

files之前添加<xsl:value-of>即可获得所需的输出
<xsl:value-of select="/files/file/@originalfilename"/>

但是,我建议使用

<xsl:value-of select="../../../@originalfilename"/>

而不是绝对路径,因此如果您有更多文件,它仍然有效。

答案 1 :(得分:1)

您想要@originalfilename的第一个<file>祖先的<Comment>

<xsl:template match="Comment">
  <feedback 
    id="{count(preceding::Comments)+1}"
    file="{ancestor::file[1]/@originalfilename}"
  >
    <xsl:value-of select="." />
  </feedback>
</xsl:template>

请注意属性值模板(花括号)。他们可以节省很多打字。

相关问题