我需要返回ID属性在<Settings></Settings>
和<Profile></Profile>
之间匹配且其<Setting/>
节点的Value
属性为true
的所有XML节点。
我的XML结构的一个例子如下:
<HBDI>
<Settings>
<Setting ID="PreferenceCode" Visible="true"/>
<Setting ID="SequenceCode" Visible="true"/>
<Setting ID="QuadrantScores" Visible="false"/>
<Setting ID="AdjectivePairs" Visible="false"/>
<Setting ID="ModeScores" Visible="true"/>
<Setting ID="Graph" Visible="true"/>
</Settings>
<Profile>
<PreferenceCode ID="PreferenceCode">3332</PreferenceCode>
<SequenceCode ID="SequenceCode">DCAB</SequenceCode>
<Graph ID="Graph">Graph.jpg</Graph>
<QuadrantScores ID="QuadrantScores">
<QuadrantScore Name="A" Value="3" />
<QuadrantScore Name="B" Value="3" />
<QuadrantScore Name="C" Value="23" />
<QuadrantScore Name="D" Value="46" />
</QuadrantScores>
<AdjectivePairs ID="AdjectivePairs">
<AdjectivePair Name="A" Value="4" />
<AdjectivePair Name="B" Value="7" />
<AdjectivePair Name="C" Value="9" />
<AdjectivePair Name="D" Value="10" />
</AdjectivePairs>
<ModeScores ID="ModeScores">
<ModeScore Name="Lt" Value="34" />
<ModeScore Name="Rt" Value="54" />
<ModeScore Name="Upr" Value="3" />
<ModeScore Name="Lwr" Value="24" />
</ModeScores>
</Profile>
</HBDI>
所以我想要实现的是检索以下xml:
<HBDI>
<PreferenceCode ID="PreferenceCode">3332</PreferenceCode>
<SequenceCode ID="SequenceCode">DCAB</SequenceCode>
<Graph ID="Graph">Graph.jpg</Graph>
<ModeScores ID="ModeScores">
<ModeScore Name="Lt" Value="34" />
<ModeScore Name="Rt" Value="54" />
<ModeScore Name="Upr" Value="3" />
<ModeScore Name="Lwr" Value="24" />
</ModeScores>
</HBDI>
因此,对于ID="PreferenceCode"
,Visible
中对应的<Settings/>
属性为true
,因此需要返回。我必须承认,我不知道如何在ID之间进行匹配。有什么帮助吗?
答案 0 :(得分:2)
在处理XML时,请使用XQuery:
DECLARE @MyXML xml = '
<HBDI>
<Settings>
<Setting ID="PreferenceCode" Visible="true"/>
<Setting ID="SequenceCode" Visible="true"/>
<Setting ID="QuadrantScores" Visible="false"/>
<Setting ID="AdjectivePairs" Visible="false"/>
<Setting ID="ModeScores" Visible="true"/>
<Setting ID="Graph" Visible="true"/>
</Settings>
<Profile>
<PreferenceCode ID="PreferenceCode">3332</PreferenceCode>
<SequenceCode ID="SequenceCode">DCAB</SequenceCode>
<Graph ID="Graph">Graph.jpg</Graph>
<QuadrantScores ID="QuadrantScores">
<QuadrantScore Name="A" Value="3" />
<QuadrantScore Name="B" Value="3" />
<QuadrantScore Name="C" Value="23" />
<QuadrantScore Name="D" Value="46" />
</QuadrantScores>
<AdjectivePairs ID="AdjectivePairs">
<AdjectivePair Name="A" Value="4" />
<AdjectivePair Name="B" Value="7" />
<AdjectivePair Name="C" Value="9" />
<AdjectivePair Name="D" Value="10" />
</AdjectivePairs>
<ModeScores ID="ModeScores">
<ModeScore Name="Lt" Value="34" />
<ModeScore Name="Rt" Value="54" />
<ModeScore Name="Upr" Value="3" />
<ModeScore Name="Lwr" Value="24" />
</ModeScores>
</Profile>
</HBDI>'
SELECT @MyXML.query('
<HDBI> {
let $settings := /HBDI/Settings
for $element in /HBDI/Profile/*
where $settings/Setting[@ID = local-name($element) and @Visible = "true"]
return $element
} </HDBI>
')
XQuery循环遍历<Profile>
标记的每个子节点,并检查它是否在@Visible = "true"
节点中设置为<Settings>
。 local-name()
给出当前节点的名称。