我有一個SQL轉儲的CSV文件,我在BaseX 8.4中使用它。 CSV標頭包含一個扁平的SQL表結構表示。XQuery翻滾窗口:如何匹配首次出現的分組鍵?
CSV與報頭和第一行:
country_id,country_code,country_name,publisher_id,publisher_name,country id,year_began,year_ended,series_id,series_name,sort_name,publisher_id
2,us,United States,78,Harvard University Press,2,1950,NULL,15,A New Series,New Series,78
爲baseX CSV解析器產生以下XML表示:
<csv>
<record>
<country_id>2</country_id>
<country_code>us</country_code>
<country_name>United States</country_name>
<publisher_id>78</publisher_id>
<publisher_name>Harvard University Press</publisher_name>
<country_id>2</country_id>
<year_began>1950</year_began>
<year_ended>NULL</year_ended>
<series_id>15</series_id>
<series_name>A New Series</series_name>
<sort_name>New Series</sort_name>
<publisher_id>78</publisher_id>
</record>
</csv>
關於原始數據,我知道一個表的起點開始它的唯一ID,但這些ID名稱也會在其他表中作爲外鍵重複出現。
我想通過匹配表的唯一ID的第一次出現(忽略每個後續事件)來創建窗口/組,重建原始表結構。我至今不能工作,因爲它的ID的每一次出現,不只是第一個匹配:
輸出:
<tables>
<table id_name="country_id">
<country_id>2</country_id>
<country_code>us</country_code>
<country_name>United States</country_name>
</table>
<table id_name="publisher_id">
<publisher_id>78</publisher_id>
<publisher_name>Harvard University Press</publisher_name>
</table>
<table id_name="country_id">
<country_id>2</country_id>
<year_began>1950</year_began>
<year_ended>NULL</year_ended>
</table>
<table id_name="series_id">
<series_id>15</series_id>
<series_name>A New Series</series_name>
<sort_name>New Series</sort_name>
</table>
<table id_name="publisher_id">
<publisher_id>78</publisher_id>
</table>
</tables>
所需的輸出:
<tables>
<table id_name="country_id">
<country_id>2</country_id>
<country_code>us</country_code>
<country_name>United States</country_name>
</table>
<table id_name="publisher_id">
<publisher_id>78</publisher_id>
<publisher_name>Harvard University Press</publisher_name>
<country_id>2</country_id>
<year_began>1950</year_began>
<year_ended>NULL</year_ended>
</table>
<table id_name="series_id">
<series_id>15</series_id>
<series_name>A New Series</series_name>
<sort_name>New Series</sort_name>
<publisher_id>78</publisher_id>
</table>
</tables>
這將有助於向我們展示了一些實際的XML輸入(最好是簡化的,我們不不需要遍歷所有15列),而不是期望我們從CSV中轉換出來時看起來像什麼樣子。您當前的查詢在遇到特定元素名稱時會啓動一個新組,並且我看不到與您的問題描述之間有任何相似之處。 –
@MichaelKay XML輸入添加。 – tat