使用此XML输入,我无法将元素添加到特定部分。根据元素属性类型对XML进行分类
<Country>
<info enum="CTRY" name="United Sates of America" total-states="50" />
<info enum="ST" name="New York" population="8,244,910"/>
<info enum="ST" name="Chicago" population="2,707,120"/>
<info enum="CTRY" name="Germany" total-states="16"/>
<info enum="ST" name="Berlin" population="3,469,910"/>
<info enum="ST" name="Brandenburg" population="2,500,000"/>
</Country>
这里是我的XSL,
<xsl:template match="/">
<Country>
<xsl:for-each select="Country/info">
<xsl:if test="@enum='CTRY'">
<CountryInfo>
<name>Country Name: <xsl:value-of select="@name"/></name>
<districts><xsl:value-of select="@total-states"></xsl:value-of></districts>
<xsl:for-each select="/Country/info">
<xsl:if test="@enum='ST'">
<state>
<stateName>State Name: <xsl:value-of select="@name"/></stateName>
<statePop>State Population: <xsl:value-of select="@population"/></statePop>
</state>
</xsl:if>
</xsl:for-each>
</CountryInfo>
</xsl:if>
</xsl:for-each>
</Country>
</xsl:template>
的问题是,所有的状态都显示出来了这两个国家。
这里是我想生成XML输出,
<Country>
<CountryInfo>
<name>Country Name: United Sates of America</name>
<districts>50</districts>
<state>
<stateName>State Name: New York</stateName>
<statePop>State Population: 8,244,910</statePop>
</state>
<state>
<stateName>State Name: Chicago</stateName>
<statePop>State Population: 2,707,120</statePop>
</state>
</CountryInfo>
<CountryInfo>
<name>Country Name: Germany</name>
<districts>16</districts>
<state>
<stateName>State Name: Berlin</stateName>
<statePop>State Population: 3,469,910</statePop>
</state>
<state>
<stateName>State Name: Brandenburg</stateName>
<statePop>State Population: 2,500,000</statePop>
</state>
</CountryInfo>
</Country>
是否有可能使用XSLT来做到这一点?
如果你展示了你的样式表,它会有所帮助。您在文章中的所有内容都是单个模板。 – Borodin