我遇到了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>