XSLT:访问 XSLT 文件中的 xml 配置

XSLT: access the xml configuration in the XSLT file

我正在尝试打印 data:fruit 中没有的水果名称。就我而言,我希望 Pineapple, Kiwi。我正在通过 'select="document('')/*/data:fruit/attribute/@value"' 访问 XSLT xml 配置中的 data:fruit。目前我收到以下错误。

错误:

Error executing XSLT at line 24 : Exception thrown by URIResolver resolving `` against `'. Found while atomizing the first argument of fn:string-join()

请检查以下 xsltfiddle link: https://xsltfiddle.liberty-development.net/jyfAiC7/4

xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:exsl="http://exslt.org/common" 
                xmlns:set="http://exslt.org/sets" 
                xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                extension-element-prefixes="exsl" 
                version="2.0">

    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <data:fruit>        
        <attribute  value="Pineapple" />
        <attribute  value="Kiwi" />
        <attribute  value="Apple" />
        <attribute  value="Orange" />
        <attribute  value="Banana" />
        <attribute  value="Cherry" />
        <attribute  value="Peach" />
    </data:fruit>           
                
    <xsl:template match="/">

            <xsl:variable name="fruitMissing"> 
                <xsl:for-each select="document('')/*/data:fruit/attribute/@value">
                    <xsl:variable name="fruit" select="."/>
                    <xsl:if test="count(//root/details/item/line[contains(.,$fruit)])=0"><xsl:value-of select="."/>,</xsl:if>
                </xsl:for-each>
            </xsl:variable>
            
            <xsl:value-of select="$fruitMissing"/> 
    </xsl:template>
</xsl:stylesheet>

xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
     <header/>
     <details>
          <item>
               <line>14.04.22 5:04:30;Apple;Finished</line>
          </item>
              <item>
               <line>14.04.22 3:36:42;Apple;Finished</line>
          </item>
          <item>
               <line>14.04.22 4:03:47;Orange;Finished</line>
          </item>
            <item>
               <line>14.04.22 2:40:33;Orange;Finished</line>
          </item> 
          <item>
               <line>14.04.22 3:37:12;Banana;Finished</line>
          </item>
          <item>
               <line>14.04.22 3:36:59;Cherry;Finished</line>
          </item>
          <item>
               <line>14.04.22 3:36:57;Peach;Finished</line>
          </item>
     </details>
     <summary/>
     <statistics/>
     <exceptions/>
</root>

试试这个 XSLT2:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
            xmlns:exsl="http://exslt.org/common" 
            xmlns:set="http://exslt.org/sets" 
            xmlns:data="http://mulocal/host" 
            xmlns:xs="http://www.w3.org/2001/XMLSchema" 
            extension-element-prefixes="exsl" 
            version="2.0">

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<data:fruit>        
    <attribute  value="Pineapple" />
    <attribute  value="Kiwi" />
    <attribute  value="Apple" />
    <attribute  value="Orange" />
    <attribute  value="Banana" />
    <attribute  value="Cherry" />
    <attribute  value="Peach" />
</data:fruit>           
            
<xsl:template match="/">
    <xsl:variable name="varInput" select="root"/>
        <xsl:for-each select="document('')/*/data:fruit/attribute/@value">
            <xsl:variable name="fruit" select="."/>
              <xsl:if test="count($varInput/details/item/line[contains(.,$fruit)])=0">
                <xsl:value-of select="."/><xsl:text>,</xsl:text>
            </xsl:if>
         </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

结果:

<?xml version="1.0" encoding="UTF-8"?>Pineapple,Kiwi,

我建议你这样做:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:key name="existing-fruit" match="item" use="tokenize(line, ';')[2]" />

<xsl:variable name="fruit">
    <attribute value="Pineapple" />
    <attribute value="Kiwi" />
    <attribute value="Apple" />
    <attribute value="Orange" />
    <attribute value="Banana" />
    <attribute value="Cherry" />
    <attribute value="Peach" />
</xsl:variable>

<xsl:template match="/" >
    <xsl:variable name="xml" select="." />
    <xsl:value-of select="$fruit/attribute[not(key('existing-fruit', @value, $xml))]/@value" separator=","/>
</xsl:template>

</xsl:stylesheet>

除了更短、更高效之外,它还避免了 line.

中包含的其他值可能出现的误报匹配

演示:https://xsltfiddle.liberty-development.net/jyfAiC7/5