Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何添加具有新id的第二个元素?_Xml_Xslt - Fatal编程技术网

Xml 如何添加具有新id的第二个元素?

Xml 如何添加具有新id的第二个元素?,xml,xslt,Xml,Xslt,这应该相当容易,但我对XSLT是新手,很难找到解决方案。我得到了以下XML: <catalog> <book id="1"> <author>Mike</author> <title>Tomas</title> </book> </catalog> 有什么建议吗?使用应用模板: <xsl:stylesheet version="1.0" xmln

这应该相当容易,但我对XSLT是新手,很难找到解决方案。我得到了以下XML:

<catalog>
    <book id="1">
        <author>Mike</author>
        <title>Tomas</title>
    </book>
</catalog>

有什么建议吗?

使用
应用模板

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="@id">
    <xsl:attribute name="id">2</xsl:attribute>
</xsl:template>

<xsl:template match="book">
    <book id="1">
      <author>author1</author>
      <title>title1</title>
    </book>
    <xsl:copy>
       <xsl:apply-templates select="@*"/>
       <xsl:copy-of select="node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

2.
作者1
标题1
您需要添加

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

确保复制目录元素或其他元素和属性时保持不变

因此,所有建议一起生成模板

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

<xsl:template match="@id">
    <xsl:attribute name="id">2</xsl:attribute>
</xsl:template>

<xsl:template match="book">
    <book id="1">
      <author>author1</author>
      <title>title1</title>
    </book>
    <xsl:copy>
       <xsl:apply-templates select="@*"/>
       <xsl:copy-of select="node()"/>
    </xsl:copy>
</xsl:template>

2.
作者1
标题1
然后我们可以将代码缩短为

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

<xsl:template match="@id">
    <xsl:attribute name="id">2</xsl:attribute>
</xsl:template>

<xsl:template match="book">
    <book id="1">
      <author>author1</author>
      <title>title1</title>
    </book>
    <xsl:call-template name="identity"/>
</xsl:template>

2.
作者1
标题1

对不起,你说得对。我把你的代码放错地方了。。。非常感谢。只有一个问题。请解释以下内容:
为什么在
copy
元素中使用
copy of
xsl:copy
进行浅拷贝,因为您要转换属性,因此,我们可以使用
apply templates
来确保您编写的属性模板用于转换属性。内部
xsl:copy of
用于复制不需要转换的子元素。正如我在上一段代码中所做的那样,如果标识转换模板就位,我们可以缩短代码以调用它,它也可以执行相同的工作,对元素进行浅层复制,并确保通过匹配模板处理属性和子节点,这是
id
属性的模板,然后递归地为那些不需要更改的节点进行标识转换。正常处理从输入文档中获取节点,并将该输入文档转换为结果文档,而不进一步转换结果元素。您需要将结果元素存储在变量中,并显式地进行转换(在XSLT1.0中,首先需要使用
exsl:node set($var)
)。
<xsl:template match="@*|node()" name="identity">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@id">
    <xsl:attribute name="id">2</xsl:attribute>
</xsl:template>

<xsl:template match="book">
    <book id="1">
      <author>author1</author>
      <title>title1</title>
    </book>
    <xsl:call-template name="identity"/>
</xsl:template>