【问题标题】:XSLT: Check if string contains any value from another group of nodesXSLT:检查字符串是否包含来自另一组节点的任何值
【发布时间】:2021-02-26 14:16:21
【问题描述】:

我有这个 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>

我正在尝试过滤掉包含组表中存在的任何组的产品。 我想结束这个。

<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 处理器?是否支持字符串函数来标记 "GROUP1","GROUP3" 之类的值?
  • 您显示的输入不是格式正确的 XML:缺少根元素。

标签: xslt xslt-1.0


【解决方案1】:

试试这个方法:

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>

【讨论】:

  • 谢谢!你的第一个例子对我有用!
【解决方案2】:

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 上应用此转换时(片段,通过将其包含在单个顶部元素 &lt;tables&gt; 中来制作格式良好的 XML 文档):

<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 条件指令(例如&lt;xsl:if&gt;)。

  2. 没有使用&lt;xsl:for-each&gt;指令

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-28
    • 2020-07-19
    • 2013-11-12
    • 2018-10-12
    • 2011-03-18
    • 2015-09-08
    • 1970-01-01
    相关资源
    最近更新 更多