【发布时间】:2022-12-08 23:03:45
【问题描述】:
我有两个 XML 文件。
第一个 XML
<annunci>
<annuncio>
<any_info>222</any_info>
<reference>333</reference>
.
.
<lot_of_info> </lot_of_info>
.
.
<some_info>1</some_info>
<images>
</images>
</annuncio>
第二个 XML
<images>
<img>
<url>http://example.com/xml/img/333/somefile</url>
<type>0</type>
<public>1</public>
<share>1</share>
</img>
<img>
<url>http://example.com/xml-feed/img/333/somefile</url>
<type>0</type>
<public>1</public>
<share>1</share>
</img>
</images>
使用第一个文件 <reference>(示例:333)和第二个文件的 <url> 合并两个 XML,子字符串(文件夹)具有相同的编号(例如:https://example.com/xml-feed/img/333/file1 和 https://example.com/xml-feed/img/333/file2),以获得最终的 XML,如下所示:
<annunci>
<annuncio>
<any_info>222</any_info>
<reference>333</reference>
.
.
<lot_of_info> </lot_of_info>
.
.
<some_info>1</some_info>
<images>
<img>
<url>http://example.com/xml/img/333/file1</url>
<type>0</type>
<public>1</public>
<share>1</share>
</img>
<img>
<url>http://example.com/xml-feed/img/333/file2</url>
<type>0</type>
<public>1</public>
<share>1</share>
</img>
</images>
</annuncio>
我尝试这个但没有运气:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- identity template: copies all nodes and attributes as-is -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- template to add the <img> elements from the second XML file -->
<xsl:template match="/annunci/annuncio/images">
<xsl:copy>
<!-- add the <img> elements from the second XML file that have a matching <url> -->
<xsl:for-each select="document('second.xml')/images/img[contains(url, /annunci/annuncio/reference)]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
因为我只需要复制...与 annuncio/annunci/images 中匹配的 URL
【问题讨论】:
-
问题是我在 output.xml 上有所有 img 节点,而不仅仅是匹配的节点。
标签: xml xslt xml-parsing xslt-2.0