1
我試圖通過匹配屬性的值來合併兩個xml文件。 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
謝謝!它完美地工作。 – brace