我是xslt的新手,我有两个xml文件,如下所示:file1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<people-data id="test-id" timestamp="20014-03-30T09:00:00">
<person>
<id>12345</id>
<first-name>John</first-name>
</person>
<person>
<id>67890</id>
<first-name>Mike</first-name>
</person>
<person>
<id>11111</id>
<first-name>Dan</first-name>
</person>
</people-data>
第二个xml文件如下:file2.xml:
<?xml version='1.0' encoding='UTF-8'?>
<people-appointment-data>
<person-data>
<id>12345</id>
<first-name>John</first-name>
<appointments>
<appointment>
<code>5124</code>
<pass>14920329324</pass>
<states>
<state>IL</state>
<state>IN</state>
</states>
</appointment>
<appointment>
<code>1001</code>
<pass>14921119324</pass>
<states>
<state>NV</state>
<state>CA</state>
</states>
</appointment>
</appointments>
</person-data>
<person-data>
<id>67890</id>
<first-name>Mike</first-name>
<appointments>
<appointment>
<code>6666</code>
<pass>14920</pass>
<states>
<state>AK</state>
<state>MA</state>
</states>
</appointment>
</appointments>
</person-data>
</people-appointment-data>
我想用xslt实现的目的是将约会信息复制到第一个xml文件中,过滤id标签上的匹配。
这就是我期望输出的结果,如果id不匹配,file1.xml中的信息将被保留:
<?xml version="1.0" encoding="UTF-8"?>
<people-data id="test-id" timestamp="20014-03-30T09:00:00">
<person>
<id>12345</id>
<first-name>John</first-name>
<appointments>
<appointment>
<code>5124</code>
<pass>14920329324</pass>
<states>
<state>IL</state>
<state>IN</state>
</states>
</appointment>
<appointment>
<code>1001</code>
<pass>14921119324</pass>
<states>
<state>NV</state>
<state>CA</state>
</states>
</appointment>
</appointments>
</person>
<person>
<id>67890</id>
<first-name>Mike</first-name>
<appointments>
<appointment>
<code>6666</code>
<pass>14920</pass>
<states>
<state>AK</state>
<state>MA</state>
</states>
</appointment>
</appointments>
</person>
<person>
<id>11111</id>
<first-name>Dan</first-name>
</person>
</people-data>
答案 0 :(得分:0)
document()函数将允许您匹配备用文档中的值。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates select="//person"/>
</xsl:template>
<xsl:template match="person">
<xsl:copy>
<xsl:apply-templates select="*" mode="copy"/>
<xsl:variable name="id" select="id/text()"/>
<xsl:apply-templates select="document('file2.xml')//person-data[id/text()=$id]/appointments" mode="copy"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="copy">
<xsl:copy>
<xsl:apply-templates select="*|text()" mode="copy"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
我建议你这样做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="lookup-document" select="document('file2.xml')"/>
<xsl:key name="pdata" match="person-data" use="id" />
<xsl:template match="/">
<people-data>
<xsl:for-each select="people-data/person">
<person>
<xsl:variable name="id" select="id" />
<xsl:copy-of select="*"/>
<!-- switch context to lookup-document in order to use the key -->
<xsl:for-each select="$lookup-document">
<xsl:copy-of select="key('pdata', $id)/appointments"/>
</xsl:for-each>
</person>
</xsl:for-each>
</people-data>
</xsl:template>
</xsl:stylesheet>