当子具有多个值时创建多个记录。
我有一个包含父节点和子节点的XML。子节点确实有多个值。我正在尝试为每个子节点创建单独的记录,以便标记按输出中显示的顺序(Desired)。
我的XML输入是:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Records">
<xsl:for-each select="Record">
<ArcherRecord>
<AppCode>
<xsl:value-of select="Field" />
</AppCode>
<xsl:for-each select="Field/Reference">
<Location>
<xsl:value-of select="." />
</Location>
</xsl:for-each>
</ArcherRecord>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我试过的我的XSLT代码是:
<ArcherRecord>
<AppCode>SPH0</AppCode>
<Location>Alberta</Location>
</ArcherRecord>
<ArcherRecord>
<AppCode>SQV0</AppCode>
<Location>Ontario</Location>
<Location>Quebec</Location>
</ArcherRecord>
但我得到的输出是:
<?xml version="1.0" encoding="UTF-8"?>
<ArcherRecord>
<AppCode>SPH0</AppCode>
<Location>Alberta</Location>
</ArcherRecord>
<ArcherRecord>
<AppCode>SQV0</AppCode>
<Location>Ontario</Location>
</ArcherRecord>
<ArcherRecord>
<AppCode>SQV0</AppCode>
<Location>Quebec</Location>
</ArcherRecord>
而不是我想要的输出,如下所示:
private Target askForTarget() {
Target t = new Target();
Scanner input = new Scanner(System.in);
System.out.print("Please enter your target:");
t.row = input.nextInt();
t.col = input.nextInt();
return t;
}
答案 0 :(得分:0)
请试试这个:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Records">
<xsl:for-each select="Record/Field/Reference">
<ArcherRecord>
<AppCode>
<xsl:value-of select="../../Field[@type='1']" />
</AppCode>
<Location>
<xsl:value-of select="." />
</Location>
</ArcherRecord>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出结果为:
<ArcherRecord>
<AppCode>SPH0</AppCode>
<Location>Alberta</Location>
</ArcherRecord>
<ArcherRecord>
<AppCode>SQV0</AppCode>
<Location>Ontario</Location>
</ArcherRecord>
<ArcherRecord>
<AppCode>SQV0</AppCode>
<Location>Quebec</Location>
</ArcherRecord>