我遇到了XSLT和for-each的问题。所有迭代的值似乎都会被检索并分配给每次迭代的特定节点。XSLT循环 - 分配给每个特定节点的所有迭代中的值
XML
<TABLE>
<HEADER xmlns="">
<DETAIL xmlns="">
<value1 xmlns="">value1</value1>
<value2 xmlns="">value2</value2>
</DETAIL>
\t <DETAIL xmlns="">
<value1 xmlns="">value3</value1>
<value2 xmlns="">value4</value2>
</DETAIL>
</HEADER>
</TABLE>
XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<xsl:template match="*">
<xsl:element name="Json">
<xsl:for-each select="*:HEADER/*:DETAIL">
<xsl:element name="array">
<xsl:element name="test1"><xsl:value-of select="../../*:HEADER/*:DETAIL/*:value1"/></xsl:element>
<xsl:element name="test2"><xsl:value-of select="../../*:HEADER/*:DETAIL/*:value2"/></xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这是输出我得到:
实际
<?xml version="1.0" encoding="UTF-8"?>
<Json>
\t <array>
\t \t <test1>value1 value3</test1>
\t \t <test2>value2 value4</test2>
\t </array>
\t <array>
\t \t <test1>value1 value3</test1>
\t \t <test2>value2 value4</test2>
\t </array>
</Json>
预计
<?xml version="1.0" encoding="UTF-8"?>
<Json>
\t <array>
\t \t <test1>value1</test1>
\t \t <test2>value2</test2>
\t </array>
\t <array>
\t \t <test1>value3</test1>
\t \t <test2>value4</test2>
\t </array>
</Json>