(不是这样)将子节点的高级xsl转换为列表

ole*_*khr 1 python xml xslt xpath lxml

输入:

<root>
    <aa><aaa/><bbb/><ccc/><ddd/><eee/></aa>
    <bb><ggg/></bb>
</root>
Run Code Online (Sandbox Code Playgroud)

理想的输出:

<root>
    <aa>aaa<aa>
    <aa>bbb<aa>
    <aa>ccc<aa>
    <aa>ddd<aa>
    <aa>eee<aa>
    <bb>ggg</bb>
</root>
Run Code Online (Sandbox Code Playgroud)

我想出了简单的xslt,但它只是正确处理,不会创建标签列表.

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <!-- select all elements that doesn't have any child nodes (elements or text etc) -->
    <xsl:template match="//*[not(node())]">
        <xsl:value-of select="name()"/>
    </xsl:template>   
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

输出:

<root>
        <aa>aaabbbcccdddeee</aa>
        <bb>ggg</bb>
</root>
Run Code Online (Sandbox Code Playgroud)

PS它是python脚本的一部分.是否可以在python脚本中使用xslt进行此类转换?或者使用简单的xpath和python逻辑的python解决方案会更好吗?

mic*_*57k 5

一个例子不能代替解释所需转换背后的逻辑.我可以想到几种不同的方法来处理您的示例输入并获得相同的输出.

这是对你想要完成的事情的猜测(阅读评论):

XSLT 1.0

<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="/">
    <root>
        <!-- select all elements that don't have any child nodes -->
        <xsl:for-each select="//*[not(node())]">
        <!-- create an element with the name of the parent element -->
            <xsl:element name="{name(..)}">
                <xsl:value-of select="name()"/>
            </xsl:element>
        </xsl:for-each>
    </root>
</xsl:template>   

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)