SQL Server 2008 R2 SQLXML and xpath query filter not working
I have the following schema returning four projects when running 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>
I want to get:
<iati-activities>
<iati-activity>
<ProjectId>00072877</ProjectId>
</iati-activity>
</iati-activities>
But when running it does not filter. Here is the xpath-query I use:
<root xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:header sql:nullvalue="ISNULL">
<sql:开发者_StackOverflowparam 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>
This is what I get:
<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>
If I use the following query:
<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>
I get the below without the iati-acitivties element:
<iati-activity>
<ProjectId>00072877</ProjectId>
</iati-activity>
Any one know what I'm doing wrong?
I know what it is... The xpath I was using is only pointing to the element and by selecting the parent of that I get the entire tree under the parent node and not only the filtered one.
It needs to be exported fully and an xslt applied where I define the parent element manually.
<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>
精彩评论