0

我已经搜索了类似的问题,但无法提出任何可行的建议。我有以下xml我需要修改它

  <XDB>
     <ROOT>
           <KEY><ID>12345</ID><DATE>5/10/2011</DATE></KEY>
           <PERSONAL><ID>1</ID><INFO><LASTNAME>Smith</LASTNAME>...</INFO></PERSONAL>
           <CONTACT><ID>1</ID><EMAIL>asmith@yahoo.com</EMAIL>...</CONTACT>
     </ROOT>
    <ROOT>
           <KEY><ID>98765</ID><DATE>5/10/2013</DATE></KEY>
           <CONTACT><ID>2</ID><EMAIL>psmithton@yahoo.com</EMAIL>...</CONTACT>
     </ROOT>

...

  </XDB>

它需要看起来像这样:

 <XDB>
     <ROOT>
       <KEY><ID>12345</ID><DATE>5/10/2011</DATE>
           <PERSONAL><ID>1</ID><INFO><LASTNAME>Smith</LASTNAME>...</INFO></PERSONAL>
           <CONTACT><ID>1</ID><EMAIL>asmith@yahoo.com</EMAIL>...</CONTACT>
        </KEY>

     </ROOT>
    <ROOT>
       <KEY><ID>98765</ID><DATE>5/10/2013</DATE>
           <CONTACT><ID>2</ID><EMAIL>psmithton@yahoo.com</EMAIL>...</CONTACT>
       </KEY>
     </ROOT>

...

  </XDB>

我需要将 2...n 个兄弟姐妹作为第一个“关键”兄弟姐妹的孩子。本质上,我需要删除关闭 < /KEY> 并将其放在关闭 < /ROOT> 之前。我会很感激你的帮助。谢谢。

4

1 回答 1

1

遵循基于身份转换的 xslt 可以完成这项工作

<?xml version="1.0" encoding="UTF-8"?>
<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"/>

    <!-- Copy everything you find... -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <!-- ... but if you find first element inside ROOT ... -->
    <xsl:template match="ROOT/node()[1]">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
            <!-- ... copy its sibling into it ... -->
            <xsl:copy-of select="following-sibling::*" />
        </xsl:copy>
    </xsl:template>

    <!-- ignore other elements inside ROOT element since they are copied in template matching first element -->
    <xsl:template match="ROOT/node()[position() &gt; 1]" />

</xsl:stylesheet>
于 2013-08-14T05:24:26.883 回答