2

さて、ここで私が達成しようとしていることです。ディレクトリ パスとセキュリティ グループの CSV ファイルから取得した XML ドキュメントがあります。一致する Path 要素を持つノードから Group 要素とその子を取得し、それを前のノードにコピーしたいと思います。次に例を示します。

<root>
    <Folder>
        <Path>\\path\to\folder\_Shared Data\</Path>
        <Group>
            <Account>Shared_Data_RW</Account>
            <FullName></FullName>
            <AccountType>GROUP</AccountType>
            <Permission>Modify</Permission>
        </Group>
    </Folder>
    <Folder>
        <Path>\\path\to\folder\_Shared Data\</Path>
        <Group>
            <Account>Shared_Data_RO</Account>
            <FullName></FullName>
            <AccountType>GROUP</AccountType>
            <Permission>Read & Execute</Permission>
        </Group>
    </Folder>
</root>

さて、それは今のように見えます。両方のノードの Path 要素が同じであることに注意してください。私が欲しいのは、それが次のようになることです:

<root>
    <Folder>
        <Path>\\path\to\folder\_Shared Data\</Path>
        <Group>
            <Account>Shared_Data_RW</Account>
            <FullName></FullName>
            <AccountType>GROUP</AccountType>
            <Permission>Modify</Permission>
        </Group>
        <Group>
            <Account>Shared_Data_RO</Account>
            <FullName></FullName>
            <AccountType>GROUP</AccountType>
            <Permission>Read & Execute</Permission>
        </Group>
    </Folder>
</root>

2 番目のノードがなくなり、Group 要素とその子が前のノードに追加されました。

私はこの種のことにはかなり慣れていません。一般的にプログラミングとスクリプト作成には慣れていますが、これを達成するための最良の方法はわかりません。XSLT が探していることを実行できる可能性があることを確認しましたが、実際に実行したいのは、入力 XML ファイルを取得し、変更を加えてから、出力 XML ファイルを取得できるようにすることです。 jsTree を使用してツリー内の Web ページに表示します。XML を処理するための Python の ElementTree も調べましたが、探している結果を得るためにどこから始めればよいかよくわかりません。

4

1 に答える 1

1

この変換:

<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="kFolderByPath" match="Folder" use="Path"/>

 <xsl:template match="/*">
  <xsl:copy>
   <xsl:apply-templates select=
    "Folder[generate-id()=generate-id(key('kFolderByPath',Path)[1])]"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="Folder">
  <Folder>
   <xsl:copy-of select=
   "Path | key('kFolderByPath',Path)/*[not(self::Path)]"/>
  </Folder>
 </xsl:template>
</xsl:stylesheet>

(整形式に修正された) 提供された XML ドキュメントに適用すると、次のようになります。

<root>
    <Folder>
        <Path>\\path\to\folder\_Shared Data\</Path>
        <Group>
            <Account>Shared_Data_RW</Account>
            <FullName></FullName>
            <AccountType>GROUP</AccountType>
            <Permission>Modify</Permission>
        </Group>
    </Folder>
    <Folder>
        <Path>\\path\to\folder\_Shared Data\</Path>
        <Group>
            <Account>Shared_Data_RO</Account>
            <FullName></FullName>
            <AccountType>GROUP</AccountType>
            <Permission>Read &amp; Execute</Permission>
        </Group>
    </Folder>
</root>

必要な正しい結果が生成されます。

<root>
   <Folder>
      <Path>\\path\to\folder\_Shared Data\</Path>
      <Group>
         <Account>Shared_Data_RW</Account>
         <FullName/>
         <AccountType>GROUP</AccountType>
         <Permission>Modify</Permission>
      </Group>
      <Group>
         <Account>Shared_Data_RO</Account>
         <FullName/>
         <AccountType>GROUP</AccountType>
         <Permission>Read &amp; Execute</Permission>
      </Group>
   </Folder>
</root>

説明:

Muenchian グループ化方法の使用。

于 2013-03-01T03:43:57.133 に答える