非常有趣的Python赏金问题 https://stackoverflow.com/questions/37170543/recursively-search-for-parent-child-combinations-and-build-tree-in-python-and-xm我知道可以用 XSLT 1.0 来解决。请注意,这不是一个重复的问题,因为上一篇文章以 Python 方法为中心,而本文尝试使用 XSLT 解决方案来解决同一问题。下面是我的尝试,但仅限于预设数量的父/子组合,这里有四个级别深,并且有条件地遍历每个级别。
有没有办法将我的解决方案推广到任何组合级别?我了解这可能需要使用以下标记化值-->
分隔器。预期输出是当前输出,但需要动态解决方案。我包含了 Python 脚本来显示最终结果。为了明确利益冲突,我不会在上面的帖子中使用任何答案,但请您这样做!
XML Input
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<nodes>
<node name="Car" child="Engine"/>
<node name="Car" child="Wheel"/>
<node name="Engine" child="Piston"/>
<node name="Engine" child="Carb"/>
<node name="Carb" child="Bolt"/>
<node name="Spare Wheel"/>
<node name="Bolt" child="Thread"/>
<node name="Carb" child="Foat"/>
<node name="Truck" child="Engine"/>
<node name="Engine" child="Bolt"/>
<node name="Wheel" child="Hubcap"/>
</nodes>
XSLT
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="nodes">
<data>
<xsl:apply-templates select="node[not(@name=ancestor::nodes/node/@child)]"/>
</data>
</xsl:template>
<xsl:template match="node">
<xsl:variable select="@name" name="currname"/>
<xsl:variable select="@child" name="currchild"/>
<xsl:variable select="/nodes/node" name="nodeset1"/>
<xsl:variable select="/nodes/node[@name=$currchild]" name="nodeset2"/>
<xsl:variable select="/nodes/node[@name=$nodeset2/@child]" name="nodeset3"/>
<xsl:variable select="/nodes/node[@name=$nodeset3/@child]" name="nodeset4"/>
<xsl:for-each select="$nodeset2">
<xsl:variable select="@child" name="nodeset2child"/>
<xsl:for-each select="$nodeset3">
<xsl:variable select="@child" name="nodeset3child"/>
<xsl:if test="@name=$nodeset2child">
<xsl:for-each select="$nodeset4">
<xsl:if test="@name=$nodeset3child">
<xsl:value-of select="$currname"/> --> <xsl:value-of select="$currchild"/> --> <xsl:value-of select="$nodeset2child"/> --> <xsl:value-of select="$nodeset3child"/> --> <xsl:value-of select="@child"/><xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:if test="$nodeset2child!=$nodeset3/@child and $nodeset3child != $nodeset4/@name">
<xsl:value-of select="$currname"/> --> <xsl:value-of select="$currchild"/> --> <xsl:value-of select="$nodeset2child"/> --> <xsl:value-of select="$nodeset3child"/><xsl:text>
</xsl:text>
</xsl:if>
</xsl:if>
</xsl:for-each>
<xsl:if test="not($nodeset2child=$nodeset3/@child or ancestor::nodes/node[@name=$nodeset2child]/@child)">
<xsl:value-of select="$currname"/> --> <xsl:value-of select="$currchild"/> --> <xsl:value-of select="$nodeset2child"/><xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:value-of select="@name[not(ancestor::node/@child=$nodeset2/@name)]"/><xsl:text>
</xsl:text>
</xsl:template>
</xsl:transform>
XML转换后的输出
<?xml version='1.0' encoding='UTF-8'?>
<data>Car --> Engine --> Piston
Car --> Engine --> Carb --> Bolt --> Thread
Car --> Engine --> Carb --> Foat
Car --> Engine --> Bolt --> Thread
Car --> Wheel --> Hubcap
Spare Wheel
Truck --> Engine --> Piston
Truck --> Engine --> Carb --> Bolt --> Thread
Truck --> Engine --> Carb --> Foat
Truck --> Engine --> Bolt --> Thread
</data>
Python script (在转换后的输出根节点上运行 xpath)
import lxml.etree as ET
# LOAD XML AND XSL DOCS
dom = ET.parse('Input.xml')
xslt = ET.parse('XSLTScript.xsl')
# TRANSFORM XML
transform = ET.XSLT(xslt)
newdom = transform(dom)
# XPATH NEW DOM ROOT NODE (<data>)
print(newdom.xpath('/data')[0].text.replace("\n\n", "\n"))
# Car --> Engine --> Piston
# Car --> Engine --> Carb --> Bolt --> Thread
# Car --> Engine --> Carb --> Foat
# Car --> Engine --> Bolt --> Thread
# Car --> Wheel --> Hubcap
# Spare Wheel
# Truck --> Engine --> Piston
# Truck --> Engine --> Carb --> Bolt --> Thread
# Truck --> Engine --> Carb --> Foat
# Truck --> Engine --> Bolt --> Thread