使用此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