2011-09-30 88 views
1

我有以下模式運行SQL XML時,返回四個項目:SQL Server 2008 R2的SQLXML和XPath查詢過濾器不工作

<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> 

A呃,一個人知道我在做什麼錯了?

回答

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>