当子代有多个值时,创建多条记录。
我有一个包含父节点和子节点的XML。子节点确实有多个值。我正在尝试为每个子节点创建单独的记录,以便标记按照输出中显示的顺序显示(所需的)。
我的XML输入是:
<Records count="65">
<Record contentId="781435" levelId="17" levelGuid="33fceb92-9ee6-458f-81f6-5bd28e4af22e" moduleId="70" parentId="0">
<Field id="15941" guid="75b528ad-2e19-42d6-9512-87b92bbf84d0" type="1">SPH0</Field>
<Field id="15997" guid="90507e16-35a1-407e-8b27-586e3e091ac3" type="9">
<Reference id="409826">Alberta</Reference>
</Field>
</Record>
<Record contentId="783299" levelId="17" levelGuid="33fceb92-9ee6-458f-81f6-5bd28e4af22e" moduleId="70" parentId="0">
<Field id="15941" guid="75b528ad-2e19-42d6-9512-87b92bbf84d0" type="1">SQV0</Field>
<Field id="15997" guid="90507e16-35a1-407e-8b27-586e3e091ac3" type="9">
<Reference id="409187">Ontario</Reference>
<Reference id="409826">Quebec</Reference>
</Field>
</Record>
</Records>我尝试的XSLT代码是:
<?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>但我得到的输出是:
<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>发布于 2017-04-03 03:48:08
请尝试以下操作:
<?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>https://stackoverflow.com/questions/43172243
复制相似问题