如何使用XSLT连接两个XML文件?
我正在尝试连接两个xml文件,其中包含可以链接的彼此的详细信息,但我不确定如何连接 “movies.xml”如何使用XSLT连接两个XML文件?,xml,xslt,xpath,Xml,Xslt,Xpath,我正在尝试连接两个xml文件,其中包含可以链接的彼此的详细信息,但我不确定如何连接 “movies.xml” “studios.xml” 输出 <studios> <studio> <name></name> <founded></founded> <location></location> <movie> <name&g
“studios.xml”
输出
<studios>
<studio>
<name></name>
<founded></founded>
<location></location>
<movie>
<name></name>
</movie>
<movie>
<name></name>
</movie>
</studio>
</studios>
我简化了文件,但仍然有效。
我的想法是使用工作室作为基础,只是获取价值和插入,但是当涉及到将电影加入到正确的工作室时,我感到困惑
我目前尝试循环浏览每部电影,直到找到相同的匹配名称,在电影中是studio,在studios中是name
但目前的尝试不起作用,这种方法可行吗
编辑:
有一个工作室,但它可以有许多电影,在工作室有几个工作室
电影里有好几部电影。
如果一个工作室有多部电影,则可以将其添加为名称,这样每个工作室就可能有多部电影元素
我试图做的是,获取所有电影并将它们添加到正确的工作室
虚拟数据:
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之王
伟大的XML
1-1-2014
关于XML
XML2.0
5-1-2004
关于XPath
伟大的XML
5-5-2013
Studios.xml
<studios>
<studio>
<name></name>
<founded></founded>
<location></location>
</studio>
</studios>
<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
1-1-1999
美国
XML2.0
9-9-1998
英格兰
Output.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>
伟大的XML
1-1-1999
英格兰
XSLT之王
关于XPath
XML2.0
9-9-1998
英格兰
关于XML
企图
<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
,并为您处理大部分工作。(推动而不是拉动。)
例如
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>
伟大的XML
1-1-1999
美国
XML2.0
9-9-1998
英格兰
Movies.xml(固定格式)
XSLT之王
伟大的XML
1-1-2014
关于XML
XML2.0
5-1-2004
关于XPath
伟大的XML
5-5-2013
XSLT1.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
1-1-1999
美国
XSLT之王
关于XPath
XML2.0
9-9-1998
英格兰
关于XML
以下是一个相对简单的方法:
<?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" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/studios">
<studios>
<xsl:for-each select="studio">
<studio>
<xsl:copy-of select="*"/>
<movies>
<xsl:for-each select="document('movies.xml')/movies/movie[studio=current()/name]">
<xsl:copy-of select="."/>
</xsl:for-each>
</movies>
</studio>
</xsl:for-each>
</studios>
</xsl:template>
</xsl:stylesheet>
当然,您必须首先修复movies.xml文档(正确关闭标记)。您提到了两个文件。只有两个文件吗?你的例子正确地反映了你的问题吗?是每部电影都在一个单独的文件中,还是您有类似于
…
?每个工作室可以有多部电影?每部电影有多个工作室?如果是这样,我建议您编辑您的代码,并包含一个代表这些问题的示例。花些时间来详细阐述它们。显示文件之间的关系。一个匹配的例子,一个不匹配的例子。在节点中包含文本和/或属性,等等。您简化得太多了。你能给例子添加一些内容吗?另外,你已经说过你已经尝试了一些方法,你能告诉我们你最近的改进尝试吗?好的,我们有虚拟数据。你的代码在哪里?好的,添加的代码,我的加入失败不在那里(我删除了它,而不是注释掉),但想法是在所有电影中循环,查看工作室的名称,并使用current()将其与工作室的名称进行比较。我选择这一个,因为它最接近我的方式。而且很容易理解。
<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 version="1.0" encoding="UTF-8"?>
<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:template match="/studios">
<studios>
<xsl:for-each select="studio">
<studio>
<xsl:copy-of select="*"/>
<movies>
<xsl:for-each select="document('movies.xml')/movies/movie[studio=current()/name]">
<xsl:copy-of select="."/>
</xsl:for-each>
</movies>
</studio>
</xsl:for-each>
</studios>
</xsl:template>
</xsl:stylesheet>