我有一个这种格式的 XML:
<rows>
<row>
<gSubAssetType>
<mSubAssetType>
<SubAssetType>126</SubAssetType>
<sgSubAssCou>
<sSubAssCou>
<SubAssScno>0000205282</SubAssScno>
</sSubAssCou>
<sSubAssCou>
<SubAssScno>0002000294</SubAssScno>
</sSubAssCou>
<sSubAssCou>
<SubAssScno>0000203622</SubAssScno>
</sSubAssCou>
</sgSubAssCou>
<sgTest>
<sTest>
<Campo1>Value1</Campo1>
</sTest>
<sTest>
<Campo1>Value2</Campo1>
</sTest>
</sgTest>
</mSubAssetType>
<mSubAssetType>
<SubAssetType>125</SubAssetType>
<sgSubAssCou>
<sSubAssCou>
<SubAssScno>0000209645</SubAssScno>
</sSubAssCou>
<sSubAssCou>
<SubAssScno>0000204835</SubAssScno>
</sSubAssCou>
<sSubAssCou>
<SubAssScno>0000208014</SubAssScno>
</sSubAssCou>
<sSubAssCou>
<SubAssScno>0000208854</SubAssScno>
</sSubAssCou>
</sgSubAssCou>
</mSubAssetType>
<mSubAssetType>
<SubAssetType>1</SubAssetType>
<sgSubAssCou>
<sSubAssCou>
<SubAssScno>0000208850</SubAssScno>
</sSubAssCou>
</sgSubAssCou>
</mSubAssetType>
</gSubAssetType>
</row>
</rows>
我想提取这种格式的数据:
SubAssetType_SubAssCou;1;2;0002000294;
SubAssetType_SubAssCou;1;3;0000203622;
SubAssetType_SubAssCou;2;1;0000209645;
SubAssetType_SubAssCou;2;2;0000204835;
SubAssetType_SubAssCou;2;3;0000208014;
SubAssetType_SubAssCou;2;4;0000208854;
SubAssetType_SubAssCou;3;1;0000208850;
SubAssetType_Test;1;1;Value1;
SubAssetType_Test;1;2;Value2;
<s
我想提取以(<sSubAssCou>
和)开头的节点,<sTest>
并跟踪节点在父节点中的位置以及父节点在其父节点中的位置。我可以使用一些嵌套的 for-each 来执行此操作,但我还需要使用 xsl:result-document 将输出写入文件,而 Saxon 在尝试两次输出到同一文档时会出错。因此,我尝试使用 for-each-group,但我不知道如何为每个 sSubAssCou 保留节点 mSubAssetType 的位置。有什么想法可以做到这一点吗?
这是我正在使用的模板
<xsl:template match="rows">
<xsl:for-each select="row/*[starts-with(name(),'g')]">
<xsl:variable name="tablePrefix" select="substring(name(),2)"/>
<xsl:for-each select="*[starts-with(name(),'m')]">
<xsl:variable name="mvIndex" select="position()"/>
<xsl:for-each select="*[starts-with(name(),'sg')]">
<xsl:variable name="tableSubPrefix" select="substring(name(),3)"/>
<xsl:for-each select="*[starts-with(name(),'s')]">
<xsl:variable name="svIndex" select="position()"/>
<xsl:value-of select="$tablePrefix"/>_<xsl:value-of select="$tableSubPrefix"/>;<xsl:value-of select="$mvIndex"/>;<xsl:value-of select="$svIndex"/>;<xsl:value-of select="./*"/>;
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
哪个工作正常,但如果我将其更改为使用结果文档根据表前缀和后缀写入文件,如下所示:
<xsl:template match="rows">
<xsl:for-each select="row/*[starts-with(name(),'g')]">
<xsl:variable name="tablePrefix" select="substring(name(),2)"/>
<xsl:for-each select="*[starts-with(name(),'m')]">
<xsl:variable name="mvIndex" select="position()"/>
<xsl:for-each select="*[starts-with(name(),'sg')]">
<xsl:variable name="tableSubPrefix" select="substring(name(),3)"/>
<xsl:result-document href="t_{$tablePrefix}_{$tableSubPrefix}.txt" method="text">
<xsl:for-each select="*[starts-with(name(),'s')]">
<xsl:variable name="svIndex" select="position()"/>
<xsl:value-of select="$tablePrefix"/>_<xsl:value-of select="$tableSubPrefix"/>;<xsl:value-of select="$mvIndex"/>;<xsl:value-of select="$svIndex"/>;<xsl:value-of select="./*"/>;
</xsl:for-each>
</xsl:result-document>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
然后就会失败并出现此错误
Error at xsl:result-document on line 14 of generic-extract-test.xsl:
XTDE1490: Cannot write more than one result document to the same URI:
file:/xxxx/t_SubAssetType_SubAssCou.txt
in built-in template rule
Cannot write more than one result document to the same URI: file:/xxxx/t_SubAssetType_SubAssCou.txt
似乎不能两次使用相同的结果文档。所以问题是我如何对这些节点进行分组,以便我可以按节点名称分组处理它们,但保留 mvIndex 和 svIndex