我有一个如下所示的 XML:
<root>
<name>
<elements T="BI" S="1">1</elements>
<elements T="BI" S="2">2</elements>
<elements T="BI" S="3">3</elements>
</name>
<name>
<elements T="BM" S="1">10</elements>
<elements T="BM" S="2">20</elements>
<elements T="BM" S="3">30</elements>
</name>
<name>
<elements T="XX" S="1">001</elements>
<elements T="XX" S="2">002</elements>
<elements T="XX" S="3">003</elements>
</name>
<name>
<elements T="XX" S="1">005</elements>
<elements T="XX" S="2">007</elements>
<elements T="XX" S="3">009</elements>
</name> </root>
我正在尝试使用以下 XSLT 转换将此 XML 转换为另一个 XML:
<xsl:for-each select="root">
<name_code_1>
<xsl:for-each select="name">
<xsl:for-each select="element[not(@T=../preceding-sibling::*/element/@T)]">
<xsl:if test="@T = 'XX' and @S = '1'">
<xsl:value-of select="substring(./text(),1,1000)"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</name_code_1>
<name_code_2>
<xsl:for-each select="name">
<xsl:for-each select="element[not(@T=../preceding-sibling::*/element/@T)]">
<xsl:if test="@T = 'XX' and @S = '2'">
<xsl:value-of select="substring(./text(),1,1000)"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</name_code_2>
<name_code_3>
<xsl:for-each select="name">
<xsl:for-each select="element[not(@T=../preceding-sibling::*/element/@T)]">
<xsl:if test="@T = 'XX' and @S = '3'">
<xsl:value-of select="substring(./text(),1,1000)"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</name_code_3>
<name_code_4>
<xsl:for-each select="name">
<xsl:for-each select="element[not(@T=../preceding-sibling::*/element/@T)]">
<xsl:if test="@T = 'XX' and @S = '1'">
<xsl:value-of select="substring(./text(),1,1000)"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</name_code_4>
<name_code_5>
<xsl:for-each select="name">
<xsl:for-each select="element[not(@T=../preceding-sibling::*/element/@T)]">
<xsl:if test="@T = 'XX' and @S = '2'">
<xsl:value-of select="substring(./text(),1,1000)"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</name_code_5>
<name_code_6>
<xsl:for-each select="name">
<xsl:for-each select="element[not(@T=../preceding-sibling::*/element/@T)]">
<xsl:if test="@T = 'XX' and @S = '3'">
<xsl:value-of select="substring(./text(),1,1000)"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</name_code_6> </xsl:for-each>
我想展平 T-attribute="XX" 并基于 S-attribute="1" 或 "2" 等的名称标签...将这些元素填充到 6 个不同的 XML 标签中,如下所示。上面的逻辑是行不通的。有人可以通过提供一些有关如何解决此问题的建议来帮助我。
以下是所需的最终输出:
<some_tag>
<name_code_1>001</name_code_1>
<name_code_2>002</name_code_2>
<name_code_3>003</name_code_3>
<name_code_4>005</name_code_4>
<name_code_5>007</name_code_5>
<name_code_6>009</name_code_6> </some_tag>
感谢 Qubiter