XSLT:检查字符串是否包含来自另一组节点的任何值

XSLT: Check if string contains any value from another group of nodes

我有这个XML:

  <table tableName="Groups">
    <item table="Groups">
      <column columnName="GroupID">GROUP1</column>
      <column columnName="GroupID">GROUP2</column>
    </item>
  </table>
  <table tableName="Products">
    <item table="Products">
      <column columnName="ProductID">1</column>
      <column columnName="ProductName">Product A</column>
      <column columnName="Groups">"GROUP1","GROUP2"</column>
    </item>
    <item table="EcomProducts">
      <column columnName="ProductID">2</column>
      <column columnName="ProductName">Product B</column>
      <column columnName="Groups">"GROUP3","GROUP4"</column>
    </item>
    <item table="EcomProducts">
      <column columnName="ProductID">3</column>
      <column columnName="ProductName">Product C</column>
      <column columnName="Groups">"GROUP1","GROUP3"</column>
    </item>
   </table>

我正在尝试过滤掉具有组中存在的任何组的产品-table。 我想以此结束。

<products>
 <product>
  <name>Product A</name>
  <groups>"GROUP1","GROUP2"</groups>
 </product>
 <product>
  <name>Product C</name>
  <groups>"GROUP1","GROUP3"</groups>
 </product>
</products>

到目前为止我已经有了这个 XSLT,但是由于我对 XSLT 还很陌生,所以我不知道如何使该测试语句更加动态:

<products>
    <xsl:for-each select="table[@tableName='Products']/item">
     <xsl:if test="contains(column[@columnName = 'Groups'] = 'GROUP1' and contains(column[@columnName = 'Groups'] = 'GROUP2'">
      <product>
       <name><xsl:value-of select="column[@columnName = 'ProductName']"/></name>
       <groups><xsl:value-of select="column[@columnName = 'Groups']"/></groups>
      </product>
     </xsl:if>
    </xsl:for-each>
</products>

这样试试:

XSLT 1.0

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

<xsl:template match="/root">
    <xsl:variable name="group-ids" select="table[@tableName='Groups']/item/column[@columnName='GroupID']" />
    <products>
        <xsl:for-each select="table[@tableName='Products']/item">
            <xsl:if test="$group-ids[contains(current()/column[@columnName='Groups'], concat('&quot;', ., '&quot;'))]">
                 <product>
                    <name>
                        <xsl:value-of select="column[@columnName='ProductName']"/>
                    </name>
                    <groups>
                        <xsl:value-of select="column[@columnName='Groups']"/>
                    </groups>
                </product>
            </xsl:if>
        </xsl:for-each>
    </products>
</xsl:template>

</xsl:stylesheet>

演示https://xsltfiddle.liberty-development.net/jxNakB6


但是,如果您的处理器恰好支持 EXSLT str:tokenize() 扩展功能,您可以做一些更简单的事情:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="exsl str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="grp-id" match="table[@tableName='Groups']/item/column[@columnName='GroupID']" use="concat('&quot;', ., '&quot;')" />

<xsl:template match="/root">
    <products>
        <xsl:for-each select="table[@tableName='Products']/item[key('grp-id', str:tokenize(column[@columnName='Groups'], ','))]">
            <product>
                <name>
                    <xsl:value-of select="column[@columnName='ProductName']"/>
                </name>
                <groups>
                    <xsl:value-of select="column[@columnName='Groups']"/>
                </groups>
            </product>
        </xsl:for-each>
    </products>
</xsl:template>

</xsl:stylesheet>

XSLT 1.0 解决方案,使用键:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:key name="kContainsGroup" match="item[not(@table='Groups')]"
  use="boolean(
               /*/table[@tableName='Groups']/item/column
                             [contains(current()/column[@columnName='Groups'],
                                       concat('&quot;', ., '&quot;'))]
               )"/>
 
  <xsl:template match="/">
    <products>
      <xsl:apply-templates select="key('kContainsGroup', 'true')"/>
    </products>
  </xsl:template>
  
  <xsl:template match="item">
   <product>
     <name><xsl:value-of select="column[@columnName='ProductName']"/></name>
     <groups><xsl:value-of select="column[@columnName='Groups']"/></groups>
   </product>
  </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML(片段时,通过将其包含在单个顶部元素中来制作格式良好的 XML 文档<tables>):

<tables>
    <table tableName="Groups">
        <item table="Groups">
            <column columnName="GroupID">GROUP1</column>
            <column columnName="GroupID">GROUP2</column>
        </item>
    </table>
    <table tableName="Products">
        <item table="Products">
            <column columnName="ProductID">1</column>
            <column columnName="ProductName">Product A</column>
            <column columnName="Groups">"GROUP1","GROUP2"</column>
        </item>
        <item table="EcomProducts">
            <column columnName="ProductID">2</column>
            <column columnName="ProductName">Product B</column>
            <column columnName="Groups">"GROUP3","GROUP4"</column>
        </item>
        <item table="EcomProducts">
            <column columnName="ProductID">3</column>
            <column columnName="ProductName">Product C</column>
            <column columnName="Groups">"GROUP1","GROUP3"</column>
        </item>
    </table>
</tables>

产生了想要的、正确的结果:

<products>
   <product>
      <name>Product A</name>
      <groups>"GROUP1","GROUP2"</groups>
   </product>
   <product>
      <name>Product C</name>
      <groups>"GROUP1","GROUP3"</groups>
   </product>
</products>

注意:

  1. 没有使用 XSLT 条件指令(例如 <xsl:if>)。

  2. 没有使用 <xsl:for-each> 指令

  3. 解决方案以几乎纯粹的“推式”实现