仅当值存在时使用 xslt 提取和打印值

extract and print values only when they exist using xslt

来源 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:ctr tg="1000">311</mr:ctr>
    <mr:dtf tg="12000" i1="1" i2=" ">
        <mr:sbf cd="d">John Diter</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"
    xmlns:d="http://www.example.com"
    xpath-default-namespace="http://www.lc.gov/mr2/slim"
    xmlns:mr="http://www.lc.gov/mr2/slim"
    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">
      <rc>
          <xsl:apply-templates/>
      </rc>
  </xsl:template>

<xsl:template match="mr:ctr[@tg = 1000]">
<xsl:variable name="dz" as="xs:string">{tg ='1000'}!</xsl:variable><d:z xml:lang="en">{normalize-space($dz)}</d:z>
</xsl:template>

<xsl:template match="mr:dtf[@tg = 12000]">
<xsl:variable name="ds" as="xs:string">{sbf[@cd = 'a']}!</xsl:variable><d:s xml:lang="en">{normalize-space($ds)}</d:s>
<xsl:variable name="dp" as="xs:string">{sbf[@cd = 'c']}!</xsl:variable><d:p>{normalize-space($dp)}</d:p>
<xsl:variable name="dc" as="xs:string">{sbf[@cd = 'd']}!</xsl:variable><d:c>{normalize-space($dc)}</d:c>
  </xsl:template>
  

</xsl:stylesheet>

当前输出:

<?xml version="1.0" encoding="UTF-8"?>
<O-PM>
   <ListRcs>
      <rc>
         <d:z xmlns:d="http://www.example.com">false!</d:z>
         <d:s xmlns:d="http://www.example.com" xml:lang="en">!</d:s>
         <d:p xmlns:d="http://www.example.com">!</d:p>
         <d:c xmlns:d="http://www.example.com">John Diter!</d:c>
      </rc>
   </ListRcs>
</O-PM>

期望的输出:

<?xml version="1.0" encoding="UTF-8"?>
<O-PM>
   <ListRcs>
      <rc>
         <d:z xmlns:d="http://www.example.com">311</d:z>
         <d:c xmlns:d="http://www.example.com">John Diter!</d:c>
      </rc>
   </ListRcs>
</O-PM>

对于初学者,我需要将 tg = 1000 的值作为输出而不是 false, 其次,如何只打印存在的值? 在上面的示例中,只有符合条件 cd = d 的值才为真。

https://xsltfiddle.liberty-development.net/asoTKA/2

看来,这次可以只匹配自己感兴趣的元素了:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:d="http://www.example.com"
    xpath-default-namespace="http://www.lc.gov/mr2/slim"
    xmlns:mr="http://www.lc.gov/mr2/slim"
    exclude-result-prefixes="#all"
    expand-text="yes"
    version="3.0">

  <xsl:mode on-no-match="shallow-skip"/>

  <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">
      <rc>
          <xsl:apply-templates/>
      </rc>
  </xsl:template>

  <xsl:template match="mr:ctr[@tg = 1000]">
    <d:z xml:lang="en">{.}</d:z>
  </xsl:template>

  <xsl:template match="dtf[@tg = 12000]/sbf[@cd = 'd']">
      <d:c>{.}!</d:c>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/asoTKA/3

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:d="http://www.example.com"
xpath-default-namespace="http://www.lc.gov/mr2/slim"
xmlns:mr="http://www.lc.gov/mr2/slim"
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">
  <rc>
      <xsl:apply-templates/>
  </rc>
</xsl:template>

<xsl:template match="mr:ctr[@tg = 1000]">
<xsl:variable name="dz" as="xs:string"><xsl:value-of select="."/></xsl:variable>
<d:z xml:lang="en"><xsl:value-of select="normalize-space($dz)"/></d:z>
</xsl:template>

<xsl:template match="mr:dtf[@tg = 12000]">
<xsl:variable name="dc" as="xs:string">
<xsl:if test="mr:sbf[@cd = 'd']"><xsl:value-of select="normalize-space(.)"/>        </xsl:if>
</xsl:variable>
<d:c xml:lang="en"><xsl:value-of select="normalize-space($dc)"/></d:c>
  </xsl:template>


</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/asoTKA/5