通过 xslt 转换将 XML 文件转换为 output.xml

标签 xml xslt xpath

艺术品.xml 文件:

<artworks>
  <artwork>
    <title>Adoration of the Magi</title>
    <author>GHIRLANDAIO, Domenico</author>
    <date>1487</date>
    <technique>Tempera on wood, diameter: 171 cm</technique>
    <location>Galleria degli Uffizi, Florence</location>
    <form>painting</form>
    <type>religious</type>
  </artwork>
</artworks>

author.xml 文件:
<authors>
  <author>
    <name>AMADEO, Giovanni Antonio</name>
    <born-died>b. ca. 1447, Pavia, d. 1522, Milano</born-died>
    <nationality>Italian</nationality>
   <biography>Giovanni Antonio Amadeo was an Italian early Renaissance sculptor</biography>
  </author>
<authors>

输出.xml 文件:
<authors>
   <author>
      <name>AMADEO, Giovanni Antonio</name>
      <born-died>b. ca. 1447, Pavia, d. 1522, Milano</born-died>
      <nationality>Italian</nationality>
      <biography>Giovanni Antonio Amadeo was an Italian early Renaissance sculptor</biography>
     <artworks form="architecture">
        <artwork date="1473">
           <title>Faهade of the church</title>
           <technique>Marble</technique>
           <location>Certosa, Pavia</location>
        </artwork>
     </artworks>
   </author>
</authors>

artworks.xml 作品作者是外键,引用了 authors.xml 的作者
条目。

我想合并这两个 XML 文档并创建一个新的 XML 文件,其中
应为每个作者存储以下信息:姓名、出生死亡、国籍、
传记和所有艺术品。艺术品按形式分组,然后按日期排序。为了
每个艺术品、标题、技术和位置都被存储

这是具有挑战性的:)

最佳答案

一个完整的解决方案:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="authors" select="document('author.xml')" />
    <xsl:variable name="artworks" select="/artworks/artwork" />
    <xsl:key name="byNameForm" match="artworks/artwork" 
                               use="concat(author, '|', form)" />
    <xsl:template match="/">
        <authors>
            <xsl:apply-templates select="$authors/*/author" />
        </authors>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="author">
        <author>
            <xsl:apply-templates />
            <xsl:apply-templates select="$artworks[author=current()/name]" />
        </author>
    </xsl:template>
    <xsl:template match="artworks/artwork" />
    <xsl:template match="artworks/artwork[generate-id()=
            generate-id(key('byNameForm', concat(author, '|', form))[1])]">
        <artworks form="{form}">
            <xsl:apply-templates 
                select="key('byNameForm', concat(author, '|', form))"
                mode="form">
                <xsl:sort select="date" data-type="number" />
            </xsl:apply-templates>
        </artworks>
    </xsl:template>
    <xsl:template match="artworks/artwork" mode="form">
        <artwork date="{date}">
            <xsl:apply-templates select="title|technique|location" />
        </artwork>
    </xsl:template>
</xsl:stylesheet>

输入:
<artworks>
    <artwork>
        <title>Adoration of the Magi</title>
        <author>GHIRLANDAIO, Domenico</author>
        <date>1486</date>
        <technique>Tempera on wood, diameter: 171 cm</technique>
        <location>Galleria degli Uffizi, Florence</location>
        <form>painting</form>
        <type>religious</type>
    </artwork>
    <artwork>
        <title>Something</title>
        <author>AMADEO, Giovanni Antonio</author>
        <date>1484</date>
        <technique>Marble</technique>
        <location>Mars</location>
        <form>sculpture</form>
        <type>religious</type>
    </artwork>
    <artwork>
        <title>Something2</title>
        <author>AMADEO, Giovanni Antonio</author>
        <date>1487</date>
        <technique>Glue</technique>
        <location>New York</location>
        <form>sculpture</form>
        <type>secular</type>
    </artwork>
    <artwork>
        <title>Something3</title>
        <author>AMADEO, Giovanni Antonio</author>
        <date>1482</date>
        <technique>Some tech</technique>
        <location>Mars</location>
        <form>paper</form>
        <type>religious</type>
    </artwork>
</artworks>

和:
<authors>
    <author>
        <name>AMADEO, Giovanni Antonio</name>
        <born-died>b. ca. 1447, Pavia, d. 1522, Milano</born-died>
        <nationality>Italian</nationality>
        <biography>Giovanni Antonio Amadeo was an Italian early
            Renaissance sculptor</biography>
    </author>
    <author>
        <name>GHIRLANDAIO, Domenico</name>
        <born-died>b. ca. 1447, Pavia, d. 1522, Venice</born-died>
        <nationality>Italian</nationality>
        <biography>N/A</biography>
    </author>
</authors>

输出:
<authors>
    <author>
        <name>AMADEO, Giovanni Antonio</name>
        <born-died>b. ca. 1447, Pavia, d. 1522, Milano</born-died>
        <nationality>Italian</nationality>
        <biography>Giovanni Antonio Amadeo was an Italian early
            Renaissance sculptor</biography>
        <artworks form="sculpture">
            <artwork date="1484">
                <title>Something</title>
                <technique>Marble</technique>
                <location>Mars</location>
            </artwork>
            <artwork date="1487">
                <title>Something2</title>
                <technique>Glue</technique>
                <location>New York</location>
            </artwork>
        </artworks>
        <artworks form="paper">
            <artwork date="1482">
                <title>Something3</title>
                <technique>Some tech</technique>
                <location>Mars</location>
            </artwork>
        </artworks>
    </author>
    <author>
        <name>GHIRLANDAIO, Domenico</name>
        <born-died>b. ca. 1447, Pavia, d. 1522, Venice</born-died>
        <nationality>Italian</nationality>
        <biography>N/A</biography>
        <artworks form="painting">
            <artwork date="1486">
                <title>Adoration of the Magi</title>
                <technique>Tempera on wood, diameter: 171 cm</technique>
                <location>Galleria degli Uffizi, Florence</location>
            </artwork>
        </artworks>
    </author>
</authors>

编辑:更新以插入作者处理,因此即使没有任何艺术品的作者也将被包括在内。

关于通过 xslt 转换将 XML 文件转换为 output.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5774917/

相关文章:

python - 如何从表体中提取纬度值?

c# - Xsd.exe 或 Svcutil.exe 将 XSD 架构转换为类

xml - 如何使用 pom.xml 备份文件?

xml - 如何在Hive中使用横向 View explode 以获取XML数据格式?

variables - XSLT 中的条件变量选择

xslt - 结果树片段背后的基本原理是什么?

xml - xsltproc 合并 xml 文件不起作用

java - 简单的 XML 和自定义列表

xml - Powershell根据子节点值选择父xml节点并添加子元素

java - 为什么我改进的 Selenium IDE 脚本在 Selenium WebDriver 执行中表现不同?