【问题标题】:Python XML and XPath to sort things outPython XML 和 XPath 来解决问题
【发布时间】:2010-07-30 15:35:03
【问题描述】:

假设我有一个如下的 XML。

<a>
 <b>
  <c>A</c>
 </b>
 <bb>
  <c>B</c>
 </bb>
 <c>
  X
 </c>
</a>

我需要将此 XML 解析为用于 a/b/c 和 a/b'/c 的字典 X,但用于 a/c 的字典 Y。

dictionary X
X[a_b_c] = A
X[a_bb_c] = B

dictionary T
T[a_c] = X
  • 问:我想使用 XPath 在 XML 文件中为此创建一个映射文件。我怎样才能做到这一点?

我认为mapping.xml如下。

<mapping>
  <from>a/c</from><to>dictionary T<to>
  ....
</mapping>

然后使用 'a/c' 得到 X,并将其放入字典 T 中。还有更好的方法吗?

【问题讨论】:

    标签: python xml xpath


    【解决方案1】:

    也许您可以使用 XSLT 做到这一点。这个样式表:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text"/>
        <xsl:key name="dict" match="item" use="@dict"/>
        <xsl:key name="path" match="*[not(*)]" use="concat(name(../..),'/',
                                                       name(..),'/',
                                                       name())"/>
        <xsl:variable name="map">
            <item path="a/b/c" dict="X"/>
            <item path="a/bb/c" dict="X"/>
            <item path="/a/c" dict="T"/>
        </xsl:variable>
        <xsl:template match="/">
            <xsl:variable name="input" select="."/>
            <xsl:for-each select="document('')/*/xsl:variable[@name='map']/*[count(.|key('dict',@dict)[1])=1]">
                <xsl:variable name="dict" select="@dict"/>
                <xsl:variable name="path" select="../item[@dict=$dict]/@path"/>
                <xsl:value-of select="concat('dictionary ',$dict,'&#xA;')"/>
                <xsl:for-each select="$input">
                    <xsl:apply-templates select="key('path',$path)">
                        <xsl:with-param name="dict" select="$dict"/>
                    </xsl:apply-templates>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:template>
        <xsl:template match="*">
            <xsl:param name="dict"/>
            <xsl:variable name="path" select="concat(name(../..),'_',
                                                     name(..),'_',
                                                     name())"/>
            <xsl:value-of select="concat($dict,'[',
                                         translate(substring($path,
                                                             1,
                                                             1),
                                                   '_',
                                                   ''),
                                         substring($path,2),'] = ',
                                         normalize-space(.),'&#xA;')"/>
        </xsl:template>
    </xsl:stylesheet>
    

    输出:

    dictionary X
    X[a_b_c] = A
    X[a_bb_c] = B
    dictionary T
    T[a_c] = X
    

    编辑:有点漂亮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多