XSLT 副本 - 属性留空

XSLT copy-of - attribute left blank

我正在尝试修改现有的 XSLT,以在输入 XML 中包含文件节点的原始文件名属性,作为输出 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_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_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>

您只需在 <xsl:value-of>

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

不过,我建议使用

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

而不是绝对路径,所以如果你有更多的文件,它仍然有效。

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

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

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