dynamic multiple filters in xsl

2019-07-25 15:50发布

<list>
<field ows_ID="1081" Program="106;#Virginia" ProgramCategory="United States"/>
<field ows_ID="1082" Program="110;#NewYork" ProgramCategory="United States" />
<field ows_ID="1083" Program="106;#Texas;#112;#Virginia;#118;#Mass" ProgramCategory="United States" />
<field ows_ID="1084" Program="111;#Florida;#180;#Texas" ProgramCategory="United States" />
<field ows_ID="1085" Program="" ProgramCategory="Australia" />
<field ows_ID="1086" Program="122;#Sydney;#Melbourne" ProgramCategory="Australia" />
<field ows_ID="1087" Program="121;#Melbourne" ProgramCategory="Australia" />
<field ows_ID="1088" Program="118;#Mass" ProgramCategory="United States" />
<field ows_ID="1088" Program="123;#Brisbane" ProgramCategory="Australia" />


</list>

I have some xml out of which I need to find count where ProgramCategory is equal to some Country and Program should not equal to some states which I am getting dynamically from some other xml. From the other xml I am getting Program associated with each ProgramCategory and then I need to filter above xml with the condition

foreach(list/field[@ProgramCategory=$Country][not(contain(@Program,$State1][not(contain(@Program,$State2][not(contain(@Program,$State3][not(contain(@Program,$State4])

The problem is that I can get n no. of states which I am getting through for loop of other xml.

So Say, if I got from another xml that states associated with United States are Virginia, Texas and Florida then the count for United States is 2 and If I say states associate with Australia are Melbourne, Sydney and Brisbane then count is 1. I am using xsl 1.0.

标签: xslt
1条回答
smile是对你的礼貌
2楼-- · 2019-07-25 16:04

Good question, +1.

I think there isn't a single XPath 1.0 expression that evaluates to the wanted count.

Here is an XSLT 1.0 solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pFilteredStates">
  <state>Virginia</state>
  <state>Texas</state>
  <state>Florida</state>
 </xsl:param>

 <xsl:param name="pCountry" select="'United States'"/>

 <xsl:variable name="vFiltered" select=
  "document('')/*/xsl:param[@name='pFilteredStates']/*
  "/>

 <xsl:template match="/">
  <xsl:variable name="vCount">
    <xsl:apply-templates select="*"/>
  </xsl:variable>

  <xsl:value-of select="string-length($vCount)"/>
 </xsl:template>

 <xsl:template match="field">
  <xsl:if test=
  "@ProgramCategory = $pCountry
  and
   not($vFiltered[contains(current()/@Program,
                           .
                           )
                 ]
      )
  ">
   <xsl:text>1</xsl:text>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<list>
    <field ows_ID="1081" Program="106;#Virginia" ProgramCategory="United States"/>
    <field ows_ID="1082" Program="110;#NewYork" ProgramCategory="United States" />
    <field ows_ID="1083" Program="106;#Texas;#112;#Virginia;#118;#Mass" ProgramCategory="United States" />
    <field ows_ID="1084" Program="111;#Florida;#180;#Texas" ProgramCategory="United States" />
    <field ows_ID="1085" Program="" ProgramCategory="Australia" />
    <field ows_ID="1086" Program="122;#Sydney;#Melbourne" ProgramCategory="Australia" />
    <field ows_ID="1087" Program="121;#Melbourne" ProgramCategory="Australia" />
    <field ows_ID="1088" Program="118;#Mass" ProgramCategory="United States" />
    <field ows_ID="1088" Program="123;#Brisbane" ProgramCategory="Australia" />
</list>

the wanted, correct result is produced:

2

II. XPath 2.0 solution:

The following single XPath 2.0 expression:

count(
   for $f in /*/*,
       $w in $f
             [@ProgramCategory eq $pCountry
            and
              not($pFilteredStates/*
                           [contains($f/@Program, .)]
                  )
             ]
     return $w
      )

when evaluated on the same XML document (above), produces the wanted, correct result:

2
查看更多
登录 后发表回答