我需要通过匹配另一个xml文档中的不同元素来更新xml文档中的元素BrightcoveVideoID。公共元素称为LegacyId。
原始xml有10个项目(这里显示了2个项目)每个项目都包含在标签PubFactoryMetadata中。每个PubFactoryMetadata标记集都有一个LegacyID。
<?xml version="1.0" encoding="UTF-8"?>
<Content>
<PubFactoryMetadata>
<RSuite_Id>W1735_XYZ.xml</RSuite_Id>
<regular_title>Abdominal Aortic Aneurysm</regular_title>
<content_type>Video</content_type>
<content_type_subtype>Classic Patient Ed</content_type_subtype>
<LegacyId>W1735 </LegacyId>
<BrightcoveVideoID>W1735</BrightcoveVideoID>
</PubFactoryMetadata>
<PubFactoryMetadata>
<file>videos-v2_W1739_Metadata_es.xml</file>
<RSuite_Id>W1739_XYZ.xml</RSuite_Id>
<regular_title>Abdominal Aortic Aneurysm</regular_title>
<content_type>Video</content_type>
<content_type_subtype>Classic Patient Ed</content_type_subtype>
<LegacyId>W1739</LegacyId>
<BrightcoveVideoID>W1739</BrightcoveVideoID>
</PubFactoryMetadata>
</Content>
更新文件包含LegacyId和BrightcoveVideoID。我需要匹配原始和更新中的LegacyId元素,然后更新将BrightcoveVideoID复制到原始。
<?xml version="1.0" encoding="UTF-8"?>
<Content>
<PubFactoryMetadata>
<LegacyId>W1735_testering</LegacyId>
<BrightcoveVideoID>66063558001</BrightcoveVideoID>
</PubFactoryMetadata>
<PubFactoryMetadata>
<LegacyId>W1739</LegacyId>
<BrightcoveVideoID>66063562001</BrightcoveVideoID>
</PubFactoryMetadata>
<PubFactoryMetadata>
<LegacyId>K1892</LegacyId>
<BrightcoveVideoID>5273514858001</BrightcoveVideoID>
</PubFactoryMetadata>
</Content>
所需的输出是让每个PubFactoryMetadata项设置为从更新文件更新BrightcoveVideoID。
<Content>
<PubFactoryMetadata>
<RSuite_Id>W1735_XYZ.xml</RSuite_Id>
<regular_title>Abdominal Aortic Aneurysm</regular_title>
<content_type>Video</content_type>
<content_type_subtype>Classic Patient Ed</content_type_subtype>
<LegacyId>W1735</LegacyId>
<BrightcoveVideoID>66063558001</BrightcoveVideoID>
</PubFactoryMetadata>
<PubFactoryMetadata>
<file>videos-v2_W1739_Metadata_es.xml</file>
<RSuite_Id>W1739_XYZ.xml</RSuite_Id>
<regular_title>Abdominal Aortic Aneurysm</regular_title>
<content_type>Video</content_type>
<content_type_subtype>Classic Patient Ed</content_type_subtype>
<LegacyId>W1739</LegacyId>
<BrightcoveVideoID>66063562001</BrightcoveVideoID>
</Content>
这是我的XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs" version="2.0">
<!-- fileName = update information file name
updates = document to be updated -->
<xsl:param name="fileName" select="'PF_Update.xml'"/>
<xsl:param name="updates" select="document($fileName)"/>
<!-- Update_Data = orginal node tree -->
<xsl:variable name="Update_BrightCoveId" select="$updates/Content/PubFactoryMetadata/BrightcoveVideoID"/>
<!--Identity -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- In the original match -->
<xsl:template match="PubFactoryMetadata">
<xsl:copy>
LegacyId: <xsl:value-of select="LegacyId"/>
<xsl:apply-templates select="$Update_BrightCoveId" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我已经尝试了几个示例,但我还没有弄清楚如何匹配一个LegacyID,然后将原始中的BrightCoveId更新为结果集。
答案 0 :(得分:0)
首先定义一个键,使您能够在查找文档中快速查找内容:
<xsl:key name="lookup" match="PubFactoryMetadata" use="LegacyID"/>
然后像这样定义模板规则:
<xsl:template match="BrightcoveVideoID">
<xsl:copy>
<xsl:value-of
select="key('lookup', ../LegacyID, document($filename))
/BrightcoveVideoID"/>
</xsl:copy>
</xsl:template>
答案 1 :(得分:0)
效果很好,谢谢!这是样式表的完整列表。
str