将XML转换为XML,但仅显示某些元素
我需要将一个xml文档中的元数据转换为另一个xml文档中的都柏林核心元数据。下面是第一个xml文档:将XML转换为XML,但仅显示某些元素,xml,xslt,dublin-core,Xml,Xslt,Dublin Core,我需要将一个xml文档中的元数据转换为另一个xml文档中的都柏林核心元数据。下面是第一个xml文档: <?xml version="1.0" encoding="UTF-8"?> <document xmlns:xlink="http://www.w3.org/1999/xlink"> <description> <title>Letter from Waldemar Schultze to Jennie Schultze</tit
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:xlink="http://www.w3.org/1999/xlink">
<description>
<title>Letter from Waldemar Schultze to Jennie Schultze</title>
<creator type="author">
<name type="personal">Schultze, Waldemar</name>
</creator>
<date>1943-06-30</date>
<source>Special Collections and University Archives, W. E. B. Du Bois Library,
University of Massachusetts Amherst.</source>
<citation>Robert and Waldemar Schultze Papers (MS 528). Special Collections and
University Archives, W.E.B. Du Bois Library, University of
Massachusetts Amherst.</citation>
</description>
<text>
<header type="letterhead">
<imageGroup>
<image xlink:href="mums528-i001-001.png"/>
<caption>page 1</caption>
</imageGroup>
<imageGroup>
<image xlink:href="mums528-i001-002.png"/>
<caption>page 2</caption>
</imageGroup>
<organization>Unites States Disciplinary Barracks</organization>
<location>Fort Leavenworth, Kansas</location>
<date format="M/DD/YY">6/30/43</date>
<recipient>
<name type="personal">Mrs. W.J. Schultze</name>
<address>875 Richmond Av., Buffalo, N.Y.</address>
<relation>Mother</relation>
</recipient>
</header>
<body>
<salutation>Dear Mother,</salutation>
<p><line>This is the first letter I have had</line>
<line>an opportunity to write you since leaving Fort</line>
<line>Jay, and I know you must be anxious to hear from me.</line></p>
<p><line>Bob and I are both feeling as well as</line>
<line>can be expected considering our present cir-</line>
<line>cumstances. We both have high blood</line>
<line>pressure, mine has been 160/100 for the past</line>
<line>2 days, and Bob's 158/96, but my sinus</line>
<line>infection has not caused me quite so much</line>
<line>trouble since leaving N.Y. State. I believe</line>
<line>the air is dryer here and is responsible</line>
<line>for any alleviation that has taken place.</line></p>
<p><line>While a prisoner here remains in their</line>
<line>so-called 1st grade, he is able to write</line>
<line>twice a week, in second grade once a week,</line>
<line>and in third grade once a month. These</line>
<line>grades refer to classifications that ostensibly</line>
<line>are for conduct while here. It is quite possible</line>
<line>to lose a conduct rating, as I understand it,</line>
<line>by not having a perpetually rusting tin cup polished</line>
<pb n="2"/>
<line>brightly for daily inspection, although the tin plating long ago dis-</line>
<line>appeared and the cup is rusty again within 2 hours after wetting.</line></p>
<p><line>The food here is good and is well-cooked,</line>
<line>with one exception, the gravy, which is nothing but</line>
<line>flour, water, and bacon grease, Strangely enough, how-</line>
<line>ever, no condiments, not even salt, are provided on</line>
<line>the table, to the detriment of otherwise very good</line>
<line>meals. While meat here is unrationed and is plentiful,</line>
<line>toilet paper; believe it or not, is rationed. A</line>
<line>5¢ roll must last a prisoner 45 days, or else -- ?</line>
<line>Perhaps, however, a prisoner can purchase additional</line>
<line>if it should be necessary.</line></p>
<p><line>Please see that my subscriptions are transferred</line>
<line>here as soon as possible from Fort Jay. Give Florence</line>
<line>and Helen my regards, and thank Joe for his</line>
<line>efforts in my behalf in managing my business.</line>
<line>Find out from Joe how tube deliveries are at the</line>
<line>present time, first to satisfy my curiosity; and</line>
<line>also let me know if you are receiving your</line>
<line>remittance regularly from him. If he is not</line>
<line>taking care of your support in accordance with</line>
<line>the instructions I left him, I wish to know it,</line>
<line>so I can write, and correct the matter.</line>
<line>You can tell Joe to subscribe to Electronics</line>
<line>magazine for me and send it to this address</line>
<line>direct from the publisher. He should also have a copy</line>
<line>of Palmer's "Calculus for Home Study," sent me by the publisher,</line>
<line>whose name he can obtain from Ulbrichs. In future letters I'll</line>
<line>copy the "Prisoner's Handbook" issued here, and the</line>
<line>contents of the detached letter form stub.</line>
</p>
<valediction>Love, Waldemar</valediction>
</body>
</text>
瓦尔德马尔·舒尔茨给珍妮·舒尔茨的信
瓦尔德马尔舒尔茨
1943-06-30
特别收藏和大学档案,杜博伊斯图书馆,W.E.B,
麻州大学阿默斯特分校
罗伯特和瓦尔德马尔·舒尔茨论文(MS 528)。特别收藏和
大学档案馆,W.E.B.杜波伊斯图书馆,大学
马萨诸塞州阿默斯特。
第1页
第2页
美国纪律兵营
堪萨斯州利文沃思堡
6/30/43
W.J.舒尔茨夫人
纽约州布法罗市里士满大道875号。
母亲
亲爱的妈妈,
这是我收到的第一封信
离开堡垒后给你写信的机会
杰,我知道你一定很想听到我的消息
鲍勃和我都感觉很好
考虑到我们目前的cir,可以预期-
积云。我们都有高血压
压力,我的压力在过去是160/100
两天,鲍勃的158/96,但是我的鼻窦
感染并没有给我造成这么多
离开纽约州后的麻烦。我相信
这里的空气比较干燥
对于已经发生的任何缓解
而这里的一名囚犯仍在监狱里
所谓一年级,他会写字
一周两次,二年级一周一次,
三年级每月一次。这些
等级是指表面上
你在这里的时候可以做些什么。这是很有可能的
据我所知,失去品行评级,
不让一个永远生锈的锡杯抛光
用于日常检查,尽管镀锡层很久以前就被破坏了-
在润湿后2小时内,杯子再次生锈
这里的食物很好,做得很好,
除了一个例外,肉汁,它只是
面粉、水和培根油脂,奇怪的是,它们是如何产生的-
从来没有调味品,甚至盐也不提供
这张桌子,在其他方面都很好
餐。虽然这里的肉没有腌制,而且很丰富,
厕纸;信不信由你,这是定量供应。A.
5卷必须持续45天,否则--?
然而,也许囚犯可以购买额外的武器
如果有必要的话
请确保我的订阅已转移
尽快从杰伊堡过来。给佛罗伦萨
海伦,我的问候,谢谢乔的问候
代表我努力管理我的业务。
从乔那里了解一下地铁在机场的运送情况
现在,首先满足我的好奇心;和
如果您收到您的订单,也请告诉我
他定期汇款。如果他不是
按照本协议照顾您的支持
我留给他的指示我想知道,
这样我就可以写,纠正这件事了。
你可以告诉乔订阅电子产品
请帮我把杂志寄到这个地址
直接从出版商那里得到。他还应该有一份副本
出版商寄给我的Palmer的“家庭学习微积分”的,
他可以从乌尔布里希那里得到他的名字。在今后的信中,我将
复制此处发布的《囚犯手册》,以及
拆下的信件表格存根的内容。
爱你,沃尔德马尔
因为我只需要在输出xml中表示少数元素,所以我的xsl只使用了少数模板,其余的都是空的:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://relaxng.org/ns/structure/1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<dc><xsl:apply-templates select="document"></xsl:apply-templates></dc>
</xsl:template>
<xsl:template match="description/creator/name">
<creator><xsl:value-of select="."/></creator>
</xsl:template>
<xsl:template match="description/title">
<title><xsl:value-of select="."/></title>
</xsl:template>
<xsl:template match="description/date">
<date><xsl:value-of select="."/></date>
</xsl:template>
<xsl:template match="description/source">
<publisher><xsl:value-of select="."/></publisher>
</xsl:template>
<source></source>
<description></description>
<subject></subject>
<coverage></coverage>
<contributor></contributor>
<identifier></identifier>
<relation></relation>
<rights></rights>
<language></language>
<type></type>
<format></format>
文档将进行验证,正确的元数据将显示在输出都柏林核心元素中。问题是,源xml的其余部分(我不需要)也出现在
元素之后。如何使所有其他文本从输出中消失?这是由于内置模板处理所有与任何自定义模板不匹配的元素。您可以添加以下模板来修复它,只需对当前XSL进行最小的更改:
<xsl:template match="*">
<xsl:apply-templates select="*"/>
</xsl:template>
仅供参考:或只需执行以下操作:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://relaxng.org/ns/structure/1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/document">
<dc>
<creator><xsl:value-of select="description/creator/name"/></creator>
<title><xsl:value-of select="description/title"/></title>
<date><xsl:value-of select="description/date"/></date>
<publisher><xsl:value-of select="description/source"/></publisher>
<source/>
<description/>
<subject/>
<coverage/>
<contributor/>
<identifier/>
<relation/>
<rights/>
<language/>
<type/>
<format/>
</dc>
</xsl:template>
</xsl:stylesheet>
在这里,大量的模板除了降低样式表的可读性(假设每个描述元素只出现一次)之外没有任何作用
请注意,空的文本结果元素必须在模板中才能显示在输出中。这是可行的,但有原因吗(除了更干净的代码)要使用它而不是har07建议的“一网打尽”模板?@Misenus您应该问相反的问题:是否有充分的理由将模板应用于您不想要的元素,然后使用空模板处理它们?答案当然不是。即使使用您的方法,您也可以仅将模板应用于您确实需要的4个元素请注意,您没有与
文档
匹配的模板-因此内置模板规则将模板应用于文档
的所有子体。