gpt4 book ai didi

xml - 如何使用 XSLT 连接两个 XML 文件?

转载 作者:数据小太阳 更新时间:2023-10-29 02:23:57 26 4
gpt4 key购买 nike

我正在尝试连接两个 xml 文件,其中包含可以链接的彼此详细信息,但我不确定如何连接。

“电影.xml”

<movies>
<movie>
<title></title>
<studio><studio>
<date></date>
</movie>
</movies>

“工作室.xml”

<studios>
<studio>
<name></name>
<founded></founded>
<location></location>
</studio>
</studios>

输出

<studios>
<studio>
<name></name>
<founded></founded>
<location></location>
<movie>
<name></name>
</movie>
<movie>
<name></name>
</movie>
</studio>
</studios>

我已经简化了文件,但仍然有效。我的想法是使用工作室作为基础,只是获取值(value)并插入,但是当谈到将电影加入正确的工作室时,我很困惑。

我目前尝试遍历每部电影,直到找到相同的匹配名称,在电影中是studio,在工作室中是name

但是目前的尝试都不行,这种方式可行吗?

编辑:一个制片厂可以拍很多部电影,一个制片厂有几个制片厂在电影中有几部电影。如果一个工作室有不止一部电影,那么可以将其添加为名称,因此每个工作室都有可能有多个电影元素。

我正在尝试做的是,获取所有电影并将它们添加到正确的工作室。

虚拟数据:电影.xml

<movies>
<movie>
<title>Lord of XSLT</title>
<studio>Great XML<studio>
<date>1-1-2014</date>
</movie>
<movie>
<title>On the XML</title>
<studio>XML2.0<studio>
<date>5-1-2004</date>
</movie>
<movie>
<title>On the XPath</title>
<studio>Great XML<studio>
<date>5-5-2013</date>
</movie>
</movies>

工作室.xml

<studios>
<studio>
<name>Great XML</name>
<founded>1-1-1999</founded>
<location>USA</location>
</studio>
<studio>
<name>XML2.0</name>
<founded>9-9-1998</founded>
<location>ENGLAND</location>
</studio>
</studios>

输出.xml

<studios>
<studio>
<name>Great XML</name>
<founded>1-1-1999</founded>
<location>ENGLAND</location>
<movie>
<name>Lord of XSLT</name>
</movie>
<movie>
<name>On the XPath</name>
</movie>
</studio>
<studio>
<name>XML2.0</name>
<founded>9-9-1998</founded>
<location>ENGLAND</location>
<movie>
<name>On the XML</name>
</movie>
</studio>
</studios>

尝试

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/studios">

<xsl:for-each select="studio">
<studio>
<name><xsl:value-of select="name"/></name>
<founded><xsl:value-of select="founded"/></founded>
<location><xsl:value-of select="location"/></location>

<xsl:for-each select="document('movies.xml')/movies/movie">
<movies>

</movies>
</xsl:for-each>

</studio>

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

最佳答案

你的想法是正确的。我建议使用 xsl:apply-templates 而不是 xsl:for-each 并使用 identity transform为您处理大部分工作。 (推而不是拉。)

例子...

Studios.xml

<studios>
<studio>
<name>Great XML</name>
<founded>1-1-1999</founded>
<location>USA</location>
</studio>
<studio>
<name>XML2.0</name>
<founded>9-9-1998</founded>
<location>ENGLAND</location>
</studio>
</studios>

Movies.xml(固定为格式正确)

<movies>
<movie>
<title>Lord of XSLT</title>
<studio>Great XML</studio>
<date>1-1-2014</date>
</movie>
<movie>
<title>On the XML</title>
<studio>XML2.0</studio>
<date>5-1-2004</date>
</movie>
<movie>
<title>On the XPath</title>
<studio>Great XML</studio>
<date>5-5-2013</date>
</movie>
</movies>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="movies" select="document('Movies.xml')"/>

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

<xsl:template match="studio">
<xsl:copy>
<xsl:apply-templates select="@*|node()|$movies/*/movie[studio=current()/name]/title"/>
</xsl:copy>
</xsl:template>

<xsl:template match="movie/title">
<movie>
<name><xsl:value-of select="."/></name>
</movie>
</xsl:template>

</xsl:stylesheet>

输出

<studios>
<studio>
<name>Great XML</name>
<founded>1-1-1999</founded>
<location>USA</location>
<movie>
<name>Lord of XSLT</name>
</movie>
<movie>
<name>On the XPath</name>
</movie>
</studio>
<studio>
<name>XML2.0</name>
<founded>9-9-1998</founded>
<location>ENGLAND</location>
<movie>
<name>On the XML</name>
</movie>
</studio>
</studios>

关于xml - 如何使用 XSLT 连接两个 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23729146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com