合并xml匹配属性值

时间:2016-04-12 13:29:31

标签: xml xslt merge

我试图通过匹配属性的值来合并两个xml文件。 xml文件来自mysql查询,其中包含' - xml'输出

file1.xml

<?xml version="1.0"?>
<resultset statement="SELECT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <row>
   <field name="ID_editore">20</field>
   <field name="nome">Name1</field>
   <field name="biografia">Bib1</field>
   <field name="autoricat"></field>
 </row>
 <row>
   <field name="ID_editore">21</field>
   <field name="nome">Name2</field>
   <field name="biografia">Bib2</field>
   <field name="autoricat">text2</field>
 </row>
</resultset>

file2.xml

<?xml version="1.0"?>
<resultset statement="SELECT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <row>
  <field name="editore_ID">20</field>
  <field name="data">1929</field>
  <field name="indirizzo">Address 1</field>
</row>
 <row>
  <field name="editore_ID">21</field>
  <field name="data">1950</field>
  <field name="indirizzo">Address 2</field>
 </row>
</resultset>

期望合并:

<?xml version="1.0"?>
<resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" statement="SELECT">
 <row>
  <field name="ID_editore">20</field>
  <field name="nome">Name1</field>
  <field name="biografia">Bib1</field>
  <field name="autoricat"/>
  <field name="data">1929</field>
  <field name="indirizzo">Address 1</field>
 </row>
 <row>
  <field name="ID_editore">21</field>
  <field name="nome">Name2</field>
  <field name="biografia">Bib2</field>
  <field name="autoricat">text2</field>
  <field name="data">1950</field>
  <field name="indirizzo">Address 2</field>
 </row>
</resultset>

使用以下样式表我得到了错误的结果,因为我不知道如何匹配正确的属性值:

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

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

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

     <xsl:copy-of select="document('file2.xml')
            /resultset/row/field[(@name='editore_ID')=current()[@name='ID_editore']]"/>

    </xsl:copy>
 </xsl:template>

 </xsl:stylesheet>

以上样式表的错误结果:

<?xml version="1.0"?>
<resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" statement="SELECT">
 <row>
    <field name="ID_editore">20</field>
    <field name="nome">Name1</field>
    <field name="biografia">Bib1</field>
    <field name="autoricat"/>
    <field name="data">1929</field>
    <field name="indirizzo">Address 1</field>
    <field name="data">1950</field>
    <field name="indirizzo">Address 2</field>
   </row>
   <row>
    <field name="ID_editore">21</field>
    <field name="nome">Name2</field>
    <field name="biografia">Bib2</field>
    <field name="autoricat">text2</field>
    <field name="data">1929</field>
    <field name="indirizzo">Address 1</field>
    <field name="data">1950</field>
    <field name="indirizzo">Address 2</field>
   </row>
  </resultset>

我运行此命令以获取输出

xsltproc stylesheet.xsl file1.xml 

1 个答案:

答案 0 :(得分:0)

您可以尝试在xsl:copy-of中使用以下XPath表达式:

document('file2.xml.xml')
    /resultset
    /row
    /field[
        @name='editore_ID' and .=current()/field[@name='ID_editore']
    ]
    /following-sibling::field

XPath首先找到editore_ID哪个值与当前ID_editore匹配,然后返回以下所有兄弟field元素(以便editore_ID本身不会复制到输出XML)。