在更改某些属性时复制整个XML文件

在更改某些属性时复制整个XML文件,xml,xslt,Xml,Xslt,这是XML文件: <libros> <libro> <titulo>El Hobbit</titulo> <autor>J. R. Tolkien</autor> </libro> <libro> <titulo>La colmena</titulo> <autor>C. J. Cela</autor> &

这是XML文件:

<libros>
  <libro>
    <titulo>El Hobbit</titulo>
    <autor>J. R. Tolkien</autor>
  </libro>
  <libro>
    <titulo>La colmena</titulo>
    <autor>C. J. Cela</autor>
  </libro>
  <libro>
    <titulo>Guerra y Paz</titulo>
    <autor>León Tolstoi</autor>
  </libro>
</libros>

霍比特人
托尔金
拉科尔梅纳
C.J.塞拉
战争与和平
勒昂·托尔斯泰
XSL必须做的是:将标签“autor”作为属性放在“libro”中,如下所示:

<libro autor="León Tolstoi">
   <titulo>Guerra y Paz</titulo>
</libro>

战争与和平
据我所知,这是:

<xsl:template match="libros">
 <xsl:copy>
        <xsl:element name="libro">
            <xsl:attribute name="autor">
                <xsl:value-of select="//libro[1]/autor/text()"/>
            </xsl:attribute>
            <xsl:value-of select="//libro[1]/titulo/text()"/>
        </xsl:element>
        <xsl:element name="libro">
            <xsl:attribute name="autor">
                <xsl:value-of select="//libro[2]/autor/text()"/>
            </xsl:attribute>
            <xsl:value-of select="//libro[2]/titulo/text()"/>
        </xsl:element>
        <xsl:element name="libro">
            <xsl:attribute name="autor">
                <xsl:value-of select="//libro[3]/autor/text()"/>
            </xsl:attribute>
            <xsl:value-of select="//libro[3]/titulo/text()"/>
        </xsl:element>
    </xsl:copy>
</xsl:template>

输出是必须的,但是我看到了一个问题,当我有很多“libro”元素时会发生什么?在谷歌上搜索,我发现可以复制整个文档,但我还没有找到适合我需要的编辑方法:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

您应该查找以下内容:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="libro">
    <xsl:copy>
        <xsl:attribute name="autor">
            <xsl:value-of select="autor"/>
        </xsl:attribute>
        <xsl:copy-of select="titulo"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

您应该查找以下内容:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="libro">
    <xsl:copy>
        <xsl:attribute name="autor">
            <xsl:value-of select="autor"/>
        </xsl:attribute>
        <xsl:copy-of select="titulo"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

您使用标识模板的方法是正确的-这确实会将整个输入文档按原样复制到输出,但这样做而不是使用单个的
的意义在于,您可以覆盖要以不同方式处理的特定节点的标识模板。您不会“编辑”标识模板本身,而是为特殊情况在其旁边添加其他模板。在你的情况下,你需要

  • 将额外的
    autor
    属性添加到
    libro
    元素和
  • 完全抑制
    autor
    元素
对于其中的第一个,您需要一个模板,如

<xsl:template match="libro">
  <!-- {} is an "attribute value template" - an XPath expression rather than a
       literal string -->
  <libro autor="{autor}">
    <!-- continue processing all children as normal -->
    <xsl:apply-templates select="@*|node()" />
  </libro>
</xsl:template>
以下是完整的样式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="libro">
    <libro autor="{autor}">
      <xsl:apply-templates select="@*|node()" />
    </libro>
  </xsl:template>

  <xsl:template match="autor" />

</xsl:stylesheet>

您使用标识模板的方法是正确的-这确实会将整个输入文档按原样复制到输出,但这样做而不是使用单个的
的意义在于,您可以覆盖要以不同方式处理的特定节点的标识模板。您不会“编辑”标识模板本身,而是为特殊情况在其旁边添加其他模板。在你的情况下,你需要

  • 将额外的
    autor
    属性添加到
    libro
    元素和
  • 完全抑制
    autor
    元素
对于其中的第一个,您需要一个模板,如

<xsl:template match="libro">
  <!-- {} is an "attribute value template" - an XPath expression rather than a
       literal string -->
  <libro autor="{autor}">
    <!-- continue processing all children as normal -->
    <xsl:apply-templates select="@*|node()" />
  </libro>
</xsl:template>
以下是完整的样式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="libro">
    <libro autor="{autor}">
      <xsl:apply-templates select="@*|node()" />
    </libro>
  </xsl:template>

  <xsl:template match="autor" />

</xsl:stylesheet>


必须使用两个模板吗?您不能仅使用1吗?@Alpha2k使用XSLT可以通过多种方法获得相同的结果。在遍历节点时,递归方法往往更简单、更有效。如果您仅限于一个模板,那么很多时候您必须定义变量,为每个循环使用,并选择/If块,这会使样式表更长、效率更低、更难阅读。递归模板没有那么复杂。花时间和精力学习如何使用它们是值得的!必须使用2个模板吗?您不能仅使用1吗?@Alpha2k使用XSLT可以通过多种方法获得相同的结果。在遍历节点时,递归方法往往更简单、更有效。如果您仅限于一个模板,那么很多时候您必须定义变量,为每个循环使用,并选择/If块,这会使样式表更长、效率更低、更难阅读。递归模板没有那么复杂。花时间和精力学习如何使用它们是值得的!