Xslt区分不同的值,并将元素动态添加到现有xml中
我需要使用xslt更改现有xml的格式。我能够获得所有不同的ID,但在设置不同ID的价格和国家元素时遇到问题,并且遇到问题。需要转换下面的xmlXslt区分不同的值,并将元素动态添加到现有xml中,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我需要使用xslt更改现有xml的格式。我能够获得所有不同的ID,但在设置不同ID的价格和国家元素时遇到问题,并且遇到问题。需要转换下面的xml <result> <data> <ID>7705379</ID> <value>54</value> <country>lv</country> <price>24.99&l
<result>
<data>
<ID>7705379</ID>
<value>54</value>
<country>lv</country>
<price>24.99</price>
</data>
<data>
<ID>7705379</ID>
<value>54</value>
<country>fi</country>
<price>24.99</price>
</data>
<data>
<ID>7111111</ID>
<value>503</value>
<country>fi</country>
<price>19.99</price>
</data>
<data>
<ID>7111111</ID>
<value>503</value>
<country>se</country>
<price>119.99</price>
</data>
</result>
7705379
54
吕
24.99
7705379
54
fi
24.99
7111111
503
fi
19.99
7111111
503
东南方
119.99
看起来像这样的东西
<data>
<Product>
<Fields>
<ID>7705379</ID>
<ProductST>
<Key id="SE" >
<Value>54</Value>
</Key>
</ProductST>
<ProductPrice>
<Key id="lv" >
<Value>24.99</Value>
</Key>
<Key id="fi" >
<Value>24.99</Value>
</Key>
</ProductPrice>
</Fields>
</Product>
<Product>
<Fields>
<ID>7111111</ID>
<ProductST>
<Key id="SE" >
<Value>503</Value>
</Key>
</ProductST>
<ProductPrice>
<Key id="fi" >
<Value>19.99</Value>
</Key>
<Key id="se" >
<Value>119.99</Value>
</Key>
</ProductPrice>
</Fields>
</Product>
</data>
7705379
54
24.99
24.99
7111111
503
19.99
119.99
基于共享的输入XML,共享的输出XML中存在一些差异。很难在评论中列出它们,因此将它们包括在解决方案中
元素对应的值与输出的少数
元素不匹配
在输出中有一个
子元素,其属性id
的值为SE
,该值在输入XML中不存在
具有正确数量的子
元素,这些元素的id
属性值与输入的
值在4个实例中的3个实例中匹配<xsl:key name="kId" match="data" use="ID" />
<xsl:template match="result">
<data>
<xsl:for-each select="data[generate-id() = generate-id(key('kId', ID)[1])]">
<Product>
<Fields>
<ID><xsl:value-of select="key('kId', ID)/ID" /></ID>
<ProductST>
<Key>
<xsl:attribute name="id">
<xsl:value-of select="'SE'" />
</xsl:attribute>
<Value><xsl:value-of select="key('kId', ID)/value" /></Value>
</Key>
</ProductST>
<ProductPrice>
<xsl:for-each select="key('kId', ID)">
<Key>
<xsl:attribute name="id">
<xsl:value-of select="country" />
</xsl:attribute>
<Value><xsl:value-of select="price" /></Value>
</Key>
</xsl:for-each>
</ProductPrice>
</Fields>
</Product>
</xsl:for-each>
</data>
</xsl:template>
这将生成以下输出
<data>
<Product>
<Fields>
<ID>7705379</ID>
<ProductST>
<Key id="SE">
<Value>54</Value>
</Key>
</ProductST>
<ProductPrice>
<Key id="lv">
<Value>24.99</Value>
</Key>
<Key id="fi">
<Value>24.99</Value>
</Key>
</ProductPrice>
</Fields>
</Product>
<Product>
<Fields>
<ID>7111111</ID>
<ProductST>
<Key id="SE">
<Value>503</Value>
</Key>
</ProductST>
<ProductPrice>
<Key id="fi">
<Value>19.99</Value>
</Key>
<Key id="se">
<Value>119.99</Value>
</Key>
</ProductPrice>
</Fields>
</Product>
</data>
7705379
54
24.99
24.99
7111111
503
19.99
119.99
编辑:解决方案已根据评论进行更新。您能分享您编写的XSLT吗?元素
的属性id
与输入XML中的数据有什么关系?输入和输出XML的数据似乎不匹配。元素sk的值应该用作元素的属性id/>很抱歉,我的问题不太清楚。很高兴在我更新的输出xm.l中发现差异。关于第2点。我也不太清楚ProductST>元素应始终具有一个子元素,其共享编码id为“SE”。但是你已经帮了我很多。谢谢。我已经相应地更新了解决方案。现在您将有一个子
,其属性为id=“SE”
(硬编码)用于
和相应的
。希望这有帮助。谢谢你在这里真的帮助了我。我会接受你的服装。不幸的是,我不能提高投票率,因为我的评分很低,但一旦我的排名更高,我会回来并提高投票率。