【发布时间】:2021-08-23 18:28:04
【问题描述】:
输入xml
<?xml version="1.0" encoding="UTF-8" ?>
<mr:collection
xmlns:mr="http://www.lc.gov/mr2/slim"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.lc.gov/mr2/slim http://www.lc.gov/standards/mrxml/schema/mr21slim.xsd">
<mr:rc>
<mr:dtf tg="2000" i1="1" i2=" ">
<mr:sbf cd="a">Christoph Kolumbus</mr:sbf>
<mr:sbf cd="d">John Diter</mr:sbf>
<mr:sbf cd="b">Julie Nat</mr:sbf>
<mr:sbf cd="f">Darius Milhaud</mr:sbf>
<mr:sbf cd="g">Erich kleiber</mr:sbf>
<mr:sbf cd="g">Franz Ludwig Horth</mr:sbf>
</mr:dtf>
<mr:dtf tg="3000" i1="1" i2=" ">
<mr:sbf cd="a">Christoph Kolumbus</mr:sbf>
<mr:sbf cd="d">Serg</mr:sbf>
<mr:sbf cd="b">Mak</mr:sbf>
<mr:sbf cd="f">DarMil</mr:sbf>
<mr:sbf cd="g">Erikl</mr:sbf>
<mr:sbf cd="g">LudHorth</mr:sbf>
</mr:dtf>
</mr:rc>
<mr:rc>
<mr:dtf tg="2000" i1="1" i2="0">
<mr:sbf cd="a">Chris Prante</mr:sbf>
<mr:sbf cd="e">"Chris Dietz"</mr:sbf>
</mr:dtf>
</mr:rc>
</mr:collection>
使用以下 xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="http://www.lc.gov/mr2/slim"
xmlns:e="https://example.com"
xmlns:dc="https://examples.com"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="collection">
<O-PM>
<ListRcs>
<xsl:apply-templates/>
</ListRcs>
</O-PM>
</xsl:template>
<xsl:template match="rc">
<e:rc>
<xsl:apply-templates/>
</e:rc>
</xsl:template>
<xsl:template match="dtf[@tg = 2000]">
<mdtd>
<rc>
<dc:title xml:lang="el">{sbf[@cd = 'a']} {sbf[@cd = 'b']}{sbf[@cd = 'e']!(':', .)} {sbf[@cd = 'f']!('/', .)}{(sbf[@cd = 'g'] => string-join(' ; '))!('', .)}</dc:title>
</rc>
</mdtd>
</xsl:template>
<xsl:template match="dtf[@tg != 2000]"/>
</xsl:stylesheet>
我们得到
<?xml version="1.0" encoding="UTF-8"?>
<O-PM>
<ListRcs>
<e:rc xmlns:e="https://example.com">
<mdtd>
<rc>
<dc:title xmlns:dc="https://examples.com" xml:lang="el">Christoph Kolumbus Julie Nat / Darius Milhaud Erich kleiber ; Franz Ludwig Horth</dc:title>
</rc>
</mdtd>
</e:rc>
<e:rc xmlns:e="https://example.com">
<mdtd>
<rc>
<dc:title xmlns:dc="https://examples.com" xml:lang="el">Chris Prante : "Chris Dietz" </dc:title>
</rc>
</mdtd>
</e:rc>
</ListRcs>
</O-PM>
如何获得
exclude-result-prefixes 应该适用于我们在 XSLT 代码中创建的文字结果元素...我需要声明命名空间,但我没有得到我想要的输出。
即
【问题讨论】:
标签: xslt-3.0