【问题标题】:i need to remove prefixes using xslt-3and exclude-result-prefixes我需要使用 xslt-3 和 exclude-result-prefixes 删除前缀
【发布时间】: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">&quot;Chris Dietz&quot;</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 代码中创建的文字结果元素...我需要声明命名空间,但我没有得到我想要的输出。 即 而不是 而不是 <title xmlns:dc="https://examples.com" xml:lang="el">

【问题讨论】:

    标签: xslt-3.0


    【解决方案1】:

    如果你使用

    <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="xs"
    expand-text="yes"
    version="3.0">
    

    即不要排除您为序列化声明的前缀edc,您将获得一个声明它们的根元素,例如

    <O-PM xmlns:dc="https://examples.com" xmlns:e="https://example.com">
    

    和类似的内容

      <e:rc>
         <mdtd>
            <rc>
               <dc:title xml:lang="el">Christoph Kolumbus Julie Nat / Darius Milhaud Erich kleiber ; Franz Ludwig Horth</dc:title>
            </rc>
         </mdtd>
      </e:rc>
    

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      相关资源
      最近更新 更多