SQL Server 2008 R2 SQLXML和xpath查询过滤器无法正常工作

时间:2011-09-30 10:28:10

标签: sqlxml

运行sql xml时,我有以下模式返回四个项目:

<xsd:element name="iati-activities" sql:relation="IATI.ACTIVITIES" sql:key-fields="BUSINESS_UNIT">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="iati-activity" sql:relation="IATI.PROJECT_METADATA" sql:relationship="ACTIVITIES_ACTIVITY">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="ProjectId" type="xsd:string" sql:field="PROJECT_ID" sql:key-fields="PROJECT_ID" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  <xsd:complexType>
</xsd:element>

我想得到:

<iati-activities>
  <iati-activity>
    <ProjectId>00072877</ProjectId>
  </iati-activity>
</iati-activities>

但是在运行时它不会过滤。这是我使用的xpath查询:

<root xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <sql:header sql:nullvalue="ISNULL">
    <sql:param name="ProjectId">00072877</sql:param>
  </sql:header>
  <sql:xpath-query mapping-schema="activity_schema.xsd">
    iati-activities[iati-activity/ProjectId=$ProjectId]
  </sql:xpath-query>
</root>

这就是我得到的:

    <iati-activities>
      <iati-activity>
        <ProjectId>00072877</ProjectId>
      </iati-activity>
      <iati-activity>
        <ProjectId>00059626</ProjectId>
      </iati-activity>
      <iati-activity>
        <ProjectId>...</ProjectId>
      </iati-activity>
      <iati-activity>
        <ProjectId>...</ProjectId>
      </iati-activity>
    </iati-activities>

如果我使用以下查询:

<root xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <sql:header sql:nullvalue="ISNULL">
    <sql:param name="ProjectId">00072877</sql:param>
  </sql:header>
  <sql:xpath-query mapping-schema="activity_schema.xsd">
    iati-activities/iati-activity[ProjectId=$ProjectId]
  </sql:xpath-query>
</root>

我在没有iati-acitivties元素的情况下得到以下内容:

<iati-activity>
  <ProjectId>00072877</ProjectId>
</iati-activity>

任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

我知道它是什么...我使用的xpath只是指向元素,通过选择父元素,我得到父节点下的整个树,而不仅仅是过滤的。

需要完全导出并在我手动定义父元素的地方应用xslt。

<xsl:template match="/">
  <iati-activities version="1.01" GENERATED_DATE="{$GENERATED_DATE}">
    <xsl:for-each select="/iati-activities/iati-activity[ProjectId=$ProjectId]">
      <xsl:copy-of select="." />
    </xsl:for-each>
  </iati-activities>
</xsl:template>