在XSLT中过滤两个不同的集合

时间:2015-01-12 16:20:22

标签: xml xslt

我有以下两个集合作为输入,我需要找到不属于输入活动的资源。

输入1:

<ActivityAssignments xmlns="http://www.example.org">
   <Activitys>
      <ActivityId>ActivityId13</ActivityId>
      <ActivityName>ActivityName14</ActivityName>
   </Activitys>
   <Activitys>
      <ActivityId>ActivityId15</ActivityId>
      <ActivityName>ActivityName16</ActivityName>
   </Activitys>
  </ActivityAssignments>

输入2:

<ResourceAssignments xmlns="http://www.example.org">
   <Resources>
      <ActivityId>ActivityId20</ActivityId>
      <ResourceName>ResourceName20</ResourceName>
   </Resources>
   <Resources>
      <ActivityId>ActivityId13</ActivityId>
      <ResourceName>ResourceName22</ResourceName>
   </Resources>
<Resources>
      <ActivityId>ActivityId15</ActivityId>
      <ResourceName>ResourceName23</ResourceName>
   </Resources>
  </ResourceAssignments>

我需要使用XSLT 1.0

以下的输出
<FinalResults>
<FinalResource>
<ActivityId>ActivityId20</ActivityId>
<ResourceName>ResourceName20</ResourceName>
</FinalResource>
</FinalResults>

请帮我实现上述结果。

我尝试了以下xslt,但它有重复的值。

<xsl:param name="outputVariable.payload"/>
  <xsl:template match="/">
    <xsl:for-each select="$outputVariable.payload/ns0:ResourceAssignments/ns0:Resources">
      <xsl:for-each select="/ns0:ActivityAssignments/ns0:Activitys">
        <xsl:if test="$outputVariable.payload/ns0:ResourceAssignments/ns0:Resources/ns0:ActivityId != /ns0:ActivityAssignments/ns0:Activitys/ns0:ActivityId">
          <ns0:FinalAssignments>
            <ns0:Final>
              <ns0:ActivityId>
                <xsl:value-of select="$outputVariable.payload/ns0:ResourceAssignments/ns0:Resources/ns0:ActivityId"/>
              </ns0:ActivityId>
              <ns0:ResourceName>
                <xsl:value-of select="$outputVariable.payload/ns0:ResourceAssignments/ns0:Resources/ns0:ResourceName"/>
              </ns0:ResourceName>
            </ns0:Final>
          </ns0:FinalAssignments>
        </xsl:if>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>

由于

Santosh Hemashekar

2 个答案:

答案 0 :(得分:0)

好的,假设$outputVariable.payload具有第二个示例中指定的XML,这里是解决此问题的“推式”方法:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns0="http://www.example.org"
                exclude-result-prefixes="ns0">

  <xsl:param name="outputVariable.payload" select="document('SOResA.xml')"/>

  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="@* | node()" priority="-3">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="/*">
    <FinalResults>
      <xsl:variable name="takenActivityIds" select="ns0:Activitys/ns0:ActivityId" />
      <xsl:variable name="allResources" 
                select="$outputVariable.payload/*/ns0:Resources" />
      <xsl:apply-templates 
                select="$allResources[not(ns0:ActivityId = $takenActivityIds)]" />
    </FinalResults>
  </xsl:template>

  <xsl:template match="ns0:Resources">
    <FinalResource>
      <xsl:apply-templates select="ns0:ActivityId | ns0:ResourceName" />
    </FinalResource>
  </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

没有&#34;收藏&#34;在XSLT 1.0中,只有一个&#34;输入&#34; (即由XSLT样式表处理的文件)。假设您将路径作为参数传递给第二个文件(Input2.xml),您可以执行以下操作:

XSLT 1.0

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

<xsl:param name="externalDocument" select="'file2.xml'" />

<xsl:template match="/ns0:ActivityAssignments">
    <xsl:variable name="actIDs" select="ns0:Activitys/ns0:ActivityId" />
    <FinalResults>
        <xsl:for-each select="document($externalDocument)/ns0:ResourceAssignments/ns0:Resources[not(ns0:ActivityId=$actIDs)]" >
            <FinalResource>
                <ActivityId>
                    <xsl:value-of select="ns0:ActivityId"/>
                </ActivityId>
                <ResourceName>
                    <xsl:value-of select="ns0:ResourceName"/>               
                </ResourceName>
            </FinalResource>
        </xsl:for-each>
    </FinalResults>
</xsl:template>

</xsl:stylesheet>

获得:

<?xml version="1.0" encoding="UTF-8"?>
<FinalResults>
   <FinalResource>
      <ActivityId>ActivityId20</ActivityId>
      <ResourceName>ResourceName20</ResourceName>
   </FinalResource>
</FinalResults>