Xml XSLT1.0-使用XSLT1.0如何剥离重复属性
这是我的XML-Xml XSLT1.0-使用XSLT1.0如何剥离重复属性,xml,xslt,Xml,Xslt,这是我的XML- <?xml version="1.0" encoding="UTF-8"?> <update-all-attributes> <document name="http://blah"> <price>111 USD</price> <color>red</color> <size>comfort</size&g
<?xml version="1.0" encoding="UTF-8"?>
<update-all-attributes>
<document name="http://blah">
<price>111 USD</price>
<color>red</color>
<size>comfort</size>
<cost>00012-40</cost>
<shipping>US::Ground:0.00</shipping>
<cost>00012-40</cost>
<price>111 USD</price>
<color>red</color>
<size>comfort</size>
<cost>00012-40</cost>
<shipping>US::Ground:0.00</shipping>
<cost>00012-40</cost>
<price>111 USD</price>
<color>red</color>
<size>comfort</size>
<shipping>US::Ground:0.00</shipping>
<price>111 USD</price>
<color>red</color>
<size>comfort</size>
</document>
<document name="http://blahblah">
<price>110 USD</price>
<color>blue</color>
<size>super</size>
<cost>00012-41</cost>
<shipping>US::Ground:0.01</shipping>
<cost>00012-41</cost>
<price>110 USD</price>
<color>blue</color>
<size>super</size>
<shipping>US::Ground:0.01</shipping>
<cost>00012-41</cost>
<price>110 USD</price>
<color>blue</color>
<size>super</size>
<shipping>US::Ground:0.01</shipping>
<price>110 USD</price>
<color>blue</color>
<size>super</size>
</document>
</update-all-attributes>
111美元
红色
安慰
00012-40
美国∶地面:0.00
00012-40
111美元
红色
安慰
00012-40
美国∶地面:0.00
00012-40
111美元
红色
安慰
美国∶地面:0.00
111美元
红色
安慰
110美元
蓝色
超级的
00012-41
美国∶地面:0.01
00012-41
110美元
蓝色
超级的
美国∶地面:0.01
00012-41
110美元
蓝色
超级的
美国∶地面:0.01
110美元
蓝色
超级的
我希望我的最终XML为
<?xml version="1.0" encoding="UTF-8"?>
<document name="http://blah">
<price>111 USD</price>
<color>red</color>
<size>comfort</size>
<cost>00012-40,00012-40,00012-40,00012-40</cost>
<shipping>US::Ground:0.00,US::Ground:0.00,US::Ground:0.00</shipping>
</document>
<document name="http://blahblah">
<price>110 USD</price>
<color>blue</color>
<size>super</size>
<cost>00012-41,00012-41,00012-41,00012-41</cost>
<shipping>US::Ground:0.01,US::Ground:0.01,US::Ground:0.01</shipping>
</document>
111美元
红色
安慰
00012-40,00012-40,00012-40,00012-40
美国::地面:0.00,美国::地面:0.00,美国::地面:0.00
110美元
蓝色
超级的
00012-41,00012-41,00012-41,00012-41
美国::地面:0.01,美国::地面:0.01,美国::地面:0.01
这是用于创建多值属性的xslt(感谢michael.hor257k)。它给了我其他属性的重复值-
<?xml version="1.0" encoding="UTF-8"?>
<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:strip-space elements="*"/>
<xsl:template match="document">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:copy-of select="@*|price"/>
<xsl:copy-of select="@*|color"/>
<xsl:copy-of select="@*|size"/>
<cost>
<xsl:for-each select="cost">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</cost>
<shipping>
<xsl:for-each select="shipping">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</shipping>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
,
,
如何修改XSLT 1.0以去除重复属性,并将所选属性保留为多值属性?我有多个文档元素,因此它应该适用于所有文档元素。请慢慢来,尝试理解注释和其背后的机制。如果您还有任何问题,请在评论中提问
<?xml version="1.0" encoding="UTF-8"?>
<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:strip-space elements="*"/>
<xsl:key name="element-by-name" match="document/*" use="local-name()"/>
<!-- Copy everything, unless other templates say otherwise -->
<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- Skip elements if there are elements of the same name earlier in the document.
Priority is set to 1 so that this template overrides other templates
and we don't accidentally still get duplicates. -->
<xsl:template match="*[count(. | key('element-by-name', local-name())[1]) > 1]" priority="1"/>
<!-- This template is for elements we want to be multi-value -->
<xsl:template match="shipping | cost">
<xsl:copy>
<!-- We step through all elements of the same name -->
<xsl:for-each select="key('element-by-name', local-name())">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
,
删除重复项的首选方法(在XSLT 1.0中)是。链接文章还解释了为什么它优于这种方法。@michael.hor257k你是对的,这太草率了。我猜甚至有些模式也是无效的。最好把它做好——我更新了我的答案。@Thomas——这太棒了,谢谢!我不明白-。我将通过Muenchian分组让您知道。@michael.hor257k-感谢您提供的文档链接!我会检查它。@ThomasW-这对的第二个类似副本不起作用,我如何使所有的逻辑迭代?这些不是“重复属性”。它们是具有相同元素名称的子元素。只是一个观察,但是像这样连接数据通常是一个非常糟糕的主意。任何随后读取这些成本
和配送
的内容都必须借助字符串操作来检索/查询它。您最好为每一双鞋创建一个新元素,并将单独的成本和运输元素放入其中。