代码之家  ›  专栏  ›  技术社区  ›  Iain Sproat

使用ref属性展平XML文件

  •  2
  • Iain Sproat  · 技术社区  · 16 年前

    我想用C编程地展平一个XML数据文件(注意,这不是一个模式,.xsd,文件)(因此,外部XML编辑器将无法工作,除非它有一个API)。例如,树结构:

    <root>
        <A>
            <B att="val">
                <C>
                    someData
                </C>
            </B>
        </A>
        <A>
            <B>
                 someOtherData
            </B>
            <B>
                moreData
            </B>
        </A>
    </root>
    

    我想把它展平:

    <root>
        <A>
            <B ref="b1" />
        </A>
        <A>
            <B ref="b2" />
            <B ref="b3" />
        </A>
        <B id="b1" att="val">
             <C ref="c1" />
        </B>
        <B id="b2">
            someOtherData
        </B>
        <B id="b3">
            moreData
        </B>
        <C id="c1">
             someData
        </C>
    </root>
    

    有没有一种方法可以通过C来实现这一点?

    有没有办法将平面XML转换回树结构?我想要尽可能通用的东西,这样任何XML文件都可以被扁平化。

    有一个 similar question on so 但不涉及参考文献。

    3 回复  |  直到 16 年前
        1
  •  3
  •   Dimitre Novatchev    16 年前

    这种转变 :

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:variable name="Lower" select=
      "'abcdefghijklmnopqrstuvwxyz'"
      />
    
     <xsl:variable name="vUpper" select=
      "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"
      />
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <root>
        <xsl:apply-templates select="node()"/>
        <xsl:apply-templates select="/*/*//*" mode="extract">
         <xsl:sort select="count(ancestor::*)" data-type="number"/>
        </xsl:apply-templates>
      </root>
     </xsl:template>
    
     <xsl:template match="*[ancestor::*[2]]">
       <xsl:variable name="vPos">
         <xsl:number level="any"/>
       </xsl:variable>
    
       <xsl:element name="{name()}">
         <xsl:attribute name="ref">
           <xsl:value-of select=
            "concat(translate(name(),$vUpper,$Lower),$vPos)"/>
         </xsl:attribute>
       </xsl:element>
     </xsl:template>
    
     <xsl:template match="*" mode="extract">
      <xsl:variable name="vPos">
       <xsl:number level="any"/>
      </xsl:variable>
    
      <xsl:element name="{name()}">
        <xsl:attribute name="id">
           <xsl:value-of select=
            "concat(translate(name(),$vUpper,$Lower),$vPos)"/>
        </xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:element>
     </xsl:template>
    </xsl:stylesheet>
    

    当应用于提供的XML文档时 :

    <root>
        <A>
            <B att="val">
                <C>
                    someData
                </C>
            </B>
        </A>
        <A>
            <B>
                 someOtherData
            </B>
            <B>
                moreData
            </B>
        </A>
    </root>
    

    生产 确切地 想要的,正确的结果 :

    <root>
       <A>
          <B ref="b1"/>
       </A>
       <A>
          <B ref="b2"/>
          <B ref="b3"/>
       </A>
       <B id="b1" att="val">
          <C ref="c1"/>
       </B>
       <B id="b2">
                 someOtherData
            </B>
       <B id="b3">
                moreData
            </B>
       <C id="c1">
                    someData
                </C>
    </root>
    

    反向转换是 :

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
     <xsl:key name="kElbyId" match="*" use="@id"/>
    
     <xsl:template match="node()|@*">
       <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
       </xsl:copy>
     </xsl:template>
    
     <xsl:template match="*[@ref]">
      <xsl:apply-templates mode="deepen"
           select="key('kElbyId',@ref)"/>
     </xsl:template>
    
     <xsl:template match="*[@id]"/>
     <xsl:template match="*[@id]" mode="deepen">
      <xsl:copy>
       <xsl:apply-templates
            select="@*[not(name()='id')] | node()"/>
      </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    当这种情况下,对上面的扁平化转换的结果应用反向转换时,将生成初始的XML文档。 :

    <root>
       <A>
          <B att="val">
             <C>
                    someData
                </C>
          </B>
       </A>
       <A>
          <B>
                 someOtherData
            </B>
          <B>
                moreData
            </B>
       </A>
    </root>
    
        2
  •  3
  •   user357812    16 年前

    此样式表:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:variable name="vUppercase" select="'QWERTYUIOPASDFGHJKLZXCVBNM'"/>
        <xsl:variable name="vLowercase" select="'qwertyuiopasdfghjklzxcvbnm'"/>
        <xsl:template match="*">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:if test="parent::*/parent::*">
                    <xsl:attribute name="id">
                        <xsl:value-of select="translate(name(),
                                                        $vUppercase,
                                                        $vLowercase)"/>
                        <xsl:number level="any"/>
                    </xsl:attribute>
                </xsl:if>
                <xsl:apply-templates mode="ref"/>
            </xsl:copy>
            <xsl:apply-templates/>
        </xsl:template>
        <xsl:template match="*" mode="ref">
            <xsl:copy>
                <xsl:attribute name="ref">
                    <xsl:value-of select="translate(name(),
                                                    $vUppercase,
                                                    $vLowercase)"/>
                    <xsl:number level="any"/>
                </xsl:attribute>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="text()"/>
        <xsl:template match="/*">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    输出:

    <root>
        <A>
            <B ref="b1" />
        </A>
        <B att="val" id="b1">
            <C ref="c1" />
        </B>
        <C id="c1">
                        someData
        </C>
        <A>
            <B ref="b2" />
            <B ref="b3" />
        </A>
        <B id="b2">
                     someOtherData
        </B>
        <B id="b3">
                    moreData
        </B>
    </root>
    

    反向样式表:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:key name="kElementById" match="*[@id]" use="@id"/>
        <xsl:key name="kElementByRef" match="*[@ref]" use="@ref"/>
        <xsl:template match="node()|@*" name="identity">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="*[key('kElementByRef',@id)]|
                             *[key('kElementByRef',@id)]/@id"/>
        <xsl:template match="*[@ref]">
            <xsl:for-each select="key('kElementById',@ref)">
                <xsl:call-template name="identity"/>
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
    

    输出:

    <root>
        <A>
            <B att="val">
                <C>
                    someData
                </C>
            </B>
        </A>
        <A>
            <B>
                 someOtherData
            </B>
            <B>
                moreData
            </B>
        </A>
    </root>
    
        3
  •  2
  •   LarsH    16 年前

    你最好使用@alejandro's或@dimitre's样式表,但我想在完成一个工作版本后发布我的样式表:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
       <xsl:output method="xml" indent="yes"/>
       <xsl:template match="/*">
          <xsl:copy>
             <!-- copy any non-elements -->
             <xsl:copy-of select="@* | node()[not(self::*)]"/>
             <!-- transform descendant elements -->
             <xsl:apply-templates select=".//*" mode="define" />
          </xsl:copy>
       </xsl:template>
    
       <xsl:template match="*" mode="define">
          <xsl:copy>
             <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
             <xsl:copy-of select="@*" />
             <xsl:apply-templates select="node()" />
          </xsl:copy>
       </xsl:template>
    
       <xsl:template match="*">
          <xsl:copy>
             <xsl:attribute name="ref"><xsl:value-of select="generate-id()"/></xsl:attribute>
          </xsl:copy>
       </xsl:template>
    
       <!-- Identity transform -->
       <xsl:template match="@* | node()" mode="ref">
          <xsl:copy>
             <xsl:apply-templates select="@* | node()"/>
          </xsl:copy>
       </xsl:template>
    
    </xsl:stylesheet>
    

    注:我没有试图保存

    • 您使用的特定ID模式。我的假设是你不在乎身份证是什么,只要它们是唯一和稳定的。如果这个假设不正确,前面的两个答案将显示如何根据您的模式生成ID。
    • 生成元素定义的顺序,尽管原始文档的顺序可以从我的输出中恢复。
    • 您的顶级元素没有ID属性。这将是一个很容易添加的特性,正如其他答案所做的那样。但希望它不是必需的:顶级元素是可以识别的,因为没有对它们的引用。

    在您的示例输入上运行样式表时,我会得到这个输出(间距很难看,但由于您有其他好的答案,我不会修正它):

    <?xml version="1.0" encoding="utf-8"?>
    <root>
    
    
    
       <A id="d0e3">
    
          <B ref="d0e5"/>
    
       </A>
       <B id="d0e5" att="val">
    
          <C ref="d0e7"/>
    
       </B>
       <C id="d0e7">
                someData
             </C>
       <A id="d0e12">
    
          <B ref="d0e14"/>
    
          <B ref="d0e17"/>
    
       </A>
       <B id="d0e14">
             someOtherData
          </B>
       <B id="d0e17">
             moreData
          </B>
    </root>