英文:
XSTL/ XSL file: need to remove duplicates generically from the parent tag given that all the child key values are same for XML
问题
以下是你提供的内容的翻译部分:
我已经在一个问题上工作了很长时间。我需要根据子标签的键值从xml文件中删除重复项。父标签"A"将始终是已知的并保持不变。嵌套标签可以有不同的名称,即可能有"Name","Location","Name"等。如果两个"Name"标签下的数据彼此重复,则必须删除一个名称标签及其子节点。只有在所有子标签键值相同的情况下,才应执行此操作,如果只有一个、两个或更多标签相同,但在父标签下存在一些具有不同键值或相同键值但不同值的标签,则不应执行此操作。
示例:
<A>
<Name>
<c>1</c>
<d>g</d>
<e>h</e>
</Name>
<Location>
<c>2</c>
<d>g</d>
<e>h</e>
</Location>
<Name>
<c>1</c>
<d>g</d>
<e>h</e>
</Name>
<A>
预期输出:
<A>
<Name>
<c>1</c>
<d>g</d>
<e>h</e>
</Name>
<Locaiton>
<c>2</c>
<d>g</d>
<e>h</e>
</Locaiton>
<A>
我尝试了这个:
<xsl:template match="@*|node()">
<xsl:if test="not(node()) or not(preceding-sibling::node()[.=string(current())])">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
但最终发生的是,具有相同键值的子标签也被删除了,我得到了这样的结果:
<A>
<Name>
<c>1</c>
<d>g</d>
<e>h</e>
</Name>
<Location>
<c>2</c>
</Location>
<A>
我正在寻找一种通用的方法,因为我不想在文件中指定标签值或键。
提前感谢你的帮助!
英文:
I have been working on a problem for a long time. I need to remove duplicates from xml file based on the key value of the child tag. The parent tag "A" will always be known and will stay the same. The nested tags can have different names i.e., there could be "Name", "Location", "Name". If the data under 2 "Name" tags are duplicates of each other, one of the name tag along with its child nodes must get removed. This should only happen if all the child tag key values are same and not if only one or 2 or more tags are same but there exists some tags with different key value or same key and different value under the parent tag.
Example:
`<A>
<Name>
<c>1<c>
<d>g</d>
<e>h</e>
</Name>
<Location>
<c>2<c>
<d>g</d>
<e>h</e>
</Location>
<Name>
<c>1<c>
<d>g</d>
<e>h</e>
</Name>
<A>`
Expected output:
`<A>
<Name>
<c>1<c>
<d>g</d>
<e>h</e>
</Name>
<Locaiton>
<c>2<c>
<d>g</d>
<e>h</e>
</Locaiton>
<A>`
I tried : this:
`<xsl:template match="@*|node()">
<xsl:if test="not(node()) or not(preceding-sibling::node()[.=string(current())])">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>`
but what ended up happening was that the child tags with the same key values got removed as well and I was getting something like this:
`<A>
<Name>
<c>1<c>
<d>g</d>
<e>h</e>
</Name>
<Location>
<c>2<c>
</Location>
<A>`
I'm looking for a generic way as I don't want to specify the tag values or keys in the file.
Thanks in advance :)!
答案1
得分: 0
在XSLT 3中,使用for-each-group group-by
与复合键可能足够:
<xsl:template match="A">
<xsl:copy>
<xsl:for-each-group select="*" composite="yes" group-by="*">
<xsl:apply-templates select="."/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
如果孙子节点也不需要排序,则可能需要以下方式:
<xsl:for-each-group select="*" composite="yes" group-by="sort(*, (), function($c) { name($c) })">
而不是上面给出的简单group-by方式。
无论哪种方式,作为基本转换,您需要通过在XSLT的xsl:stylesheet
(或xsl:transform
)中声明<xsl:mode on-no-match="shallow-copy"/>
来设置身份转换。
但问题的规范性不太明确,不清楚子元素的名称和顺序是完全未知的还是始终相同的,或者是否可能存在变化以及如何处理它们。
作为替代方案,如果您可以让A
的子元素不同,但只需要针对特定元素(如B
)消除重复项,但可能还有其他可能的元素,那么为B
元素明确声明的键可以帮助:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:key name="B-group" match="A/B" use="sort(*, (), function($c) { name($c) })"/>
<xsl:template match="B[not(. is key('B-group', sort(*, (), function($c) { name($c) }))[1])]"/>
</xsl:stylesheet>
英文:
In XSLT 3 using a composite key with for-each-group group-by
might suffice:
<xsl:template match="A">
<xsl:copy>
<xsl:for-each-group select="*" composite="yes" group-by="*">
<xsl:apply-templates select="."/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
If the grandchildren also not to be sorted then you might need
<xsl:for-each-group select="*" composite="yes" group-by="sort(*, (), function($c) { name($c) })">
instead of the simple group-by given above.
Both ways, as the base transformation, you need to set up the identity transformation by declaring <xsl:mode on-no-match="shallow-copy"/>
as a child of xsl:stylesheet
(or xsl:transform
) in the XSLT.
But the problem is rather underspecified, it is not clear whether the names and order of child elements are simply unknown are always the same or whether there can be variations and how to handle them.
As an alternative, if you can have different elements as children of A
but need to eliminate duplicates only for a specific one like B
but for possible other elements then a key explicitly declared for B
elements can help
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:key name="B-group" match="A/B" use="sort(*, (), function($c) { name($c) })"/>
<xsl:template match="B[not(. is key('B-group', sort(*, (), function($c) { name($c) }))[1])]"/>
</xsl:stylesheet>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论