首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >2个XML文件,1个XLST:使用共享ID号提取数据

2个XML文件,1个XLST:使用共享ID号提取数据
EN

Stack Overflow用户
提问于 2022-06-14 19:56:55
回答 1查看 31关注 0票数 0

我真的很想在这里提供一些帮助。XSLT相对较新,并试图通过XSLT将2个xml文件中的信息汇总起来。在一个文件中,我有ID号和标题。在另一个文件中,我有ID号和主题标题。基于共享ID号,我希望能够在XML输出中创建两个单独的记录,列出每个项目的相关主题标题。以下是我所处的位置:

-XML文件1--

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <Details Level="1">
        <Section SectionNumber="0">
            <Field Name="IDNUMBER1">
                <Value>MMK.01922</Value>
            </Field>
            <Field Name="TITLE1">
                <Value>This is the first title</Value>
            </Field>
        </Section>
    </Details>
    <Details Level="1">
        <Section SectionNumber="0">
            <Field Name="IDNUMBER1">
                <Value>MMK.01984</Value>
            </Field>
            <Field Name="TITLE1">
                <Value>Here is the second title</Value>
            </Field>
        </Section>
    </Details>
</root>

-XML文件2

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mimsy_subject_data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <record>
        <id_number>MMK.01922</id_number>
        <subjects>
            <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
            <thesaurus_lcna_geo2>Abbotsford (Scotland)</thesaurus_lcna_geo2>
            <thesaurus_lcna_name1>Scott, Walter, 1771-1832</thesaurus_lcna_name1>
            <thesaurus_lcsh1>Scottish Borders (England and Scotland)</thesaurus_lcsh1>
        </subjects>
    </record>
    <record>
        <id_number>MMK.01984</id_number>
        <subjects>
            <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
            <thesaurus_lcna_geo2>Edinburgh (Scotland)</thesaurus_lcna_geo2>
            <thesaurus_lcna_corp1>Historic Royal Palaces (Great Britain)</thesaurus_lcna_corp1>
            <thesaurus_lcsh1>Palaces</thesaurus_lcsh1>
            <thesaurus_lcsh2>Landscapes</thesaurus_lcsh2>
            <thesaurus_lcsh3>Buildings</thesaurus_lcsh3>
        </subjects>
    </record>
</mimsy_subject_data>

-XSLT

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:str="http://exslt.org/strings"
    xmlns:functx="http://www.functx.com" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns:oai="http://www.openarchives.org/OAI/2.0/">

    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>
    <!--  <xsl:template match="/"> -->

    <xsl:template match="root">
        <marc:collection>
            <xsl:apply-templates/>
        </marc:collection>
    </xsl:template>

    <xsl:template match="Details">
        <xsl:apply-templates select="Section"/>
    </xsl:template>

    <xsl:template match="Section">

        <xsl:param name = "subjectheading" select="document('mimsy_subject_data_export_tidied_for_SO.xml')/mimsy_subject_data/record" />

        <marc:record>
            <!-- BEGIN subjects-->
         
            <xsl:for-each select=".">
                <xsl:variable name="ID" select="Field[@Name = 'IDNUMBER1']/Value" />
                <xsl:variable name="shcheck" select="$subjectheading[id_number=$ID]" />

                <xsl:if test="$shcheck">
                  <marc:datafield tag="650" ind1=" " ind2=" ">
                    <marc:subfield code="a">
                     <xsl:copy-of select="$subjectheading/subjects"/>
                   </marc:subfield>
                  </marc:datafield>
               </xsl:if>
          </xsl:for-each>
        </marc:record>
    </xsl:template>
    <xsl:template match="*"/>
</xsl:stylesheet>

--电流输出--

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<marc:collection xmlns:str="http://exslt.org/strings"
                 xmlns:functx="http://www.functx.com"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:marc="http://www.loc.gov/MARC21/slim"
                 xmlns:oai="http://www.openarchives.org/OAI/2.0/">
   <marc:record>
      <marc:datafield tag="650" ind1=" " ind2=" ">
         <marc:subfield code="a">
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Abbotsford (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_name1>Scott, Walter, 1771-1832</thesaurus_lcna_name1>
               <thesaurus_lcsh1>Scottish Borders (England and Scotland)</thesaurus_lcsh1>
            </subjects>
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Edinburgh (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_corp1>Historic Royal Palaces (Great Britain)</thesaurus_lcna_corp1>
               <thesaurus_lcsh1>Palaces</thesaurus_lcsh1>
               <thesaurus_lcsh2>Landscapes</thesaurus_lcsh2>
               <thesaurus_lcsh3>Buildings</thesaurus_lcsh3>
            </subjects>
         </marc:subfield>
      </marc:datafield>
   </marc:record>
   <marc:record>
      <marc:datafield tag="650" ind1=" " ind2=" ">
         <marc:subfield code="a">
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Abbotsford (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_name1>Scott, Walter, 1771-1832</thesaurus_lcna_name1>
               <thesaurus_lcsh1>Scottish Borders (England and Scotland)</thesaurus_lcsh1>
            </subjects>
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Edinburgh (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_corp1>Historic Royal Palaces (Great Britain)</thesaurus_lcna_corp1>
               <thesaurus_lcsh1>Palaces</thesaurus_lcsh1>
               <thesaurus_lcsh2>Landscapes</thesaurus_lcsh2>
               <thesaurus_lcsh3>Buildings</thesaurus_lcsh3>
            </subjects>
         </marc:subfield>
      </marc:datafield>
   </marc:record>
</marc:collection>

-所需产出

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<marc:collection xmlns:str="http://exslt.org/strings"
                 xmlns:functx="http://www.functx.com"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:marc="http://www.loc.gov/MARC21/slim"
                 xmlns:oai="http://www.openarchives.org/OAI/2.0/">
   <marc:record>
      <marc:datafield tag="650" ind1=" " ind2=" ">
         <marc:subfield code="a">
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Abbotsford (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_name1>Scott, Walter, 1771-1832</thesaurus_lcna_name1>
               <thesaurus_lcsh1>Scottish Borders (England and Scotland)</thesaurus_lcsh1>
            </subjects>
         </marc:subfield>
      </marc:datafield>
   </marc:record>

   <marc:record>
      <marc:datafield tag="650" ind1=" " ind2=" ">
         <marc:subfield code="a">
            <subjects>
               <thesaurus_lcna_geo1>Scotland</thesaurus_lcna_geo1>
               <thesaurus_lcna_geo2>Edinburgh (Scotland)</thesaurus_lcna_geo2>
               <thesaurus_lcna_corp1>Historic Royal Palaces (Great Britain)</thesaurus_lcna_corp1>
               <thesaurus_lcsh1>Palaces</thesaurus_lcsh1>
               <thesaurus_lcsh2>Landscapes</thesaurus_lcsh2>
               <thesaurus_lcsh3>Buildings</thesaurus_lcsh3>
            </subjects>
         </marc:subfield>
      </marc:datafield>
   </marc:record>
</marc:collection>

当前输出创建两个记录,其中包含xml文件中列出的所有主题标题。我希望输出有两个记录,其中只包括与每个ID号码相关的主题标题。我认为这可能需要我使用另一个模板,但我只是不知道如何实现。这里的任何帮助都非常感谢。事先非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-14 20:17:52

声明用于交叉引用的密钥。

代码语言:javascript
复制
<xsl:key name="id" match="record" use="id_number"/>

然后在模板中使用

代码语言:javascript
复制
<xsl:template match="Section">
  <xsl:variable name="referenced-record" select="key('id', Field[@Name = 'IDNUMBER1']/Value, doc('mimsy_subject_data_export_tidied_for_SO.xml'))"/>

只需选择/输出需要它们的主题,例如模板中的<xsl:copy-of select="$referenced-record/subjects"/>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72622711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档