我有一组独特的部分(part_number用部分节点定义。这些部分可以多次使用,每个部分由part_occurance节点定义,这是 通过零件值链接到零件ID。
<data>
<part id="part_id_01">
<part_number>number_01</part_number>
</part>
<part id="part_id_03">
<part_number>number_18</part_number>
</part>
<part id="part_id_04">
<part_number>number_12</part_number>
</part>
<part id="part_id_05">
<part_number>number_13</part_number>
</part>
<part_occurence id="occurence_id_01">
<part>part_id_03</part>
<name>test_0</name>
</part_occurence>
<part_occurence id="occurence_id_02">
<part>part_id_03</part>
<name>test_1</name>
</part_occurence>
<part_occurence id="occurence_id_03">
<part>part_id_01</part>
<name>test_2</name>
</part_occurence>
<part_occurence id="occurence_id_04">
<part>part_id_04</part>
<name>test_3</name>
</part_occurence>
<part_occurence id="occurence_id_05">
<part>part_id_03</part>
<name>test_4</name>
</part_occurence>
<part_occurence id="occurence_id_06">
<part>part_id_03</part>
<name>test_4</name>
</part_occurence>
</data>
此套装包含所有出现:
<xsl:variable name="occurrence_List1" select="part_occurrence" />
如何获取此变量包含出现但排除所有出现的part_number,例如'number_18'
我的XML非常大并且是动态创建的,我将在此变量中仅保留过滤后的数据,以便以后每次使用。因为我后来多次抛出这个变量我希望尽可能保持小的速度原因...抱歉我的英文不好:))
这是我现在的xsl文件:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pxslt="file://pxslt.py" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:key name="k1" match="part" use="@id"/>
<xsl:template match="/data">
<xsl:variable name="part_list" select="part" />
<xsl:variable name="occurrence_List1" select="part_occurrence[not(key('k1', part)/part_number = 'number_18')]" />
<!--<xsl:variable name="occurrence_List1" select="part_occurence" />-->
<test>
<xsl:for-each select="$occurrence_List1">
<part>
<xsl:variable name="idx" select="part"/>
<xsl:attribute name="part"><xsl:value-of select="part" /></xsl:attribute>
<xsl:attribute name="part_name"><xsl:value-of select="$part_list[@id=$idx]/part_number" /></xsl:attribute>
</part>
</xsl:for-each>
</test>
</xsl:template>
</xsl:stylesheet>
但结果是空的。 :(
当我尝试不过滤时,我得到:
<test xmlns:pxslt="file://pxslt.py" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<part part="part_id_03" part_name="number_18"/>
<part part="part_id_03" part_name="number_18"/>
<part part="part_id_01" part_name="number_01"/>
<part part="part_id_04" part_name="number_12"/>
<part part="part_id_03" part_name="number_18"/>
<part part="part_id_03" part_name="number_18"/>
</test>
答案 0 :(得分:0)
定义一个键
<xsl:key name="k1" match="part" use="@id"/>
然后使用<xsl:variable name="occurrence_List1" select="part_occurence[not(key('k1', part)/part_number = 'number_18')] />
。
以下是基于您提供的示例的完整示例:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pxslt="file://pxslt.py" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:key name="k1" match="part" use="@id"/>
<xsl:template match="/data">
<xsl:variable name="part_list" select="part" />
<xsl:variable name="occurrence_List1" select="part_occurence[not(key('k1', part)/part_number = 'number_18')]" />
<!--<xsl:variable name="occurrence_List1" select="part_occurence" />-->
<test>
<xsl:for-each select="$occurrence_List1">
<part part="{part}" part_name="{key('k1', part)/part_number}"/>
</xsl:for-each>
</test>
</xsl:template>
</xsl:stylesheet>
注意英文名称的拼写,您的示例XML有&#34; part_occurence&#34;当你的路径选择或试图选择&#34; part_occurrence&#34;,根据我的拼写,我认为,我现在使用了错误的拼写&#34;出现&#34;在路径中确保选择拼写错误的XML元素。