用XSLT增加XML属性? 华夫饼干 $9 加糖浆试试,额外收费*
我使用以下XSLT将第二个食物项分解为三个元素用XSLT增加XML属性? 华夫饼干 $9 加糖浆试试,额外收费*,xml,xslt,Xml,Xslt,我使用以下XSLT将第二个食物项分解为三个元素 <Menu> <Food> <Item> <Name>Waffles</Name> <Price>$9</Price> <Description>Try them with syrup *Additional Charge*</Description> </Item>
<Menu>
<Food>
<Item>
<Name>Waffles</Name>
<Price>$9</Price>
<Description>Try them with syrup *Additional Charge*</Description>
</Item>
<Item name="Pop-Tarts" price="$40" description="yummy"></Item>
<Food>
<Menu>
在上一个模板中,我如何为每个“Item”元素获取“id”属性,并为每个新项目递增它
这就是我当前输出的样子
<xsl:template match="/">
<xsl:apply-templates select="Menu/Food" />
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Item">
<xsl:element name="{name()}">
<!--This is what Im trying to fix-->
<xsl:attribute name="id"></xsl:attribute>
<xsl:for-each select="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:apply-templates select="* | text()"/>
</xsl:element>
</xsl:template>
华夫饼干
$9
加糖浆试试,额外收费*
流行馅饼
$40
好吃的
那么:
XSLT1.0
<Food>
<Item id="">
<Name>Waffles</Name>
<Price>$9</Price>
<Description>Try them with syrup *Additional Charge*</Description>
</Item>
<Item id="">
<name>Pop-Tarts</name>
<price>$40</price>
<description>yummy</description>
</Item>
</Food>
请注意,XML区分大小写:
Name
与Name
@michael.hor257k此代码最终不会在我的输出中显示任何内容
然而,在我的代码中使用这部分答案得到了我所需要的
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Menu">
<Food>
<xsl:for-each select="Food/Item">
<Item id="{position()}">
<xsl:for-each select="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:copy-of select="*"/>
</Item>
</xsl:for-each>
</Food>
</xsl:template>
</xsl:stylesheet>
.....
您可以在此处看到我的答案(输入被更正为格式良好的XML)正常工作:。
<xsl:template match="Food/Item">
<Item id="{position()}">
.....
</Item>
</xsl:template>