1

私は以下のようなxmlを持っています:

<?xml version="1.0" encoding="utf-8"?><Master>
<Account ID="38058226">
    <Property ID="66591454">
        <Profile ID="111234">\Acct_38058226\Prop_66591454\Prof_111234.xml</Profile>
    </Property>
</Account>
<Account ID="38058226">
    <Property ID="66591454">
        <Profile ID="22222222222">\Acct_38058226\Prop_66591454\Prof_22222222222.xml</Profile>
    </Property>
</Account>
<Account ID="38058226">
    <Property ID="66591455">
        <Profile ID="000000">\Acct_38058226\Prop_66591454\Prof_000000.xml</Profile>
    </Property>
</Account>
<Account ID="38058227">
    <Property ID="66591454">
        <Profile ID="000000">\Acct_38058226\Prop_66591454\Prof_000000.xml</Profile>
    </Property>
</Account>
</Master>

私が探している出力は次のとおりです。

<Master>
<Account ID="38058226">
    <Property ID="66591454">
        <Profile ID="111234">\Acct_38058226\Prop_66591454\Prof_111234.xml</Profile>
        <Profile ID="22222222222">\Acct_38058226\Prop_66591454\Prof_22222222222.xml</Profile>
    </Property>
    <Property ID="66591455">
        <Profile ID="000000">\Acct_38058226\Prop_66591454\Prof_000000.xml</Profile>
    </Property>
</Account>
<Account ID="38058227">
    <Property ID="66591454">
        <Profile ID="000000">\Acct_38058226\Prop_66591454\Prof_000000.xml</Profile>
    </Property>
</Account>
</Master>

XSLT1.0を使用しています。xmlは毎晩拡張し続けます。私はxml/xsltを初めて使用します。私はそれを研究するために私の手で何時間も費やしましたが、配達は非常に近く、したがって助けを求めています。私が得たヒントは、Muenchian Groupingを使用して、キーを形成し、グループ化する必要があるかもしれないということです。私はまだ学習中であり、自分でロジックを作成することはまだできません。前もって感謝します!!

4

1 に答える 1

2

これを行う1つの方法があります。

このXSLTの場合:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key 
    name="kAccounts"
    match="Account"
    use="@ID"/>
  <xsl:key
    name="kPropertiesByAccount"
    match="Property"
    use="parent::Account/@ID"/>
  <xsl:key
    name="kPropertiesByIdAndAccount"
    match="Property"
    use="concat(@ID, '+', parent::Account/@ID)"/>
  <xsl:key
    name="kProfileByPropertyAndAccount"
    match="Profile"
    use="concat(parent::Property/@ID, '+', ancestor::Account/@ID)"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/*">
    <Master>
      <xsl:apply-templates
        select="Account[
          generate-id() = generate-id(key('kAccounts', @ID)[1])
        ]"/>
    </Master>

  </xsl:template>

  <xsl:template match="Account">
    <xsl:copy>
      <xsl:apply-templates
        select="@*|key(
          'kPropertiesByAccount', @ID)[
            generate-id() =
            generate-id(
              key('kPropertiesByIdAndAccount',
                concat(@ID, '+', parent::Account/@ID))[1])
          ]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Property">
    <xsl:copy>
      <xsl:apply-templates
        select="@*|key(
          'kProfileByPropertyAndAccount',
           concat(@ID, '+', parent::Account/@ID))"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

...提供されたXMLに対して実行されます:

<?xml version="1.0" encoding="UTF-8"?>
<Master>
  <Account ID="38058226">
    <Property ID="66591454">
      <Profile ID="111234">\Acct_38058226\Prop_66591454\Prof_111234.xml</Profile>
    </Property>
  </Account>
  <Account ID="38058226">
    <Property ID="66591454">
      <Profile ID="22222222222">\Acct_38058226\Prop_66591454\Prof_22222222222.xml</Profile>
    </Property>
  </Account>
  <Account ID="38058226">
    <Property ID="66591455">
      <Profile ID="000000">\Acct_38058226\Prop_66591454\Prof_000000.xml</Profile>
    </Property>
  </Account>
  <Account ID="38058227">
    <Property ID="66591454">
      <Profile ID="000000">\Acct_38058226\Prop_66591454\Prof_000000.xml</Profile>
    </Property>
  </Account>
</Master>

...必要な結果が生成されます:

<Master>
  <Account ID="38058226">
    <Property ID="66591454">
      <Profile ID="111234">\Acct_38058226\Prop_66591454\Prof_111234.xml</Profile>
      <Profile ID="22222222222">\Acct_38058226\Prop_66591454\Prof_22222222222.xml</Profile>
    </Property>
    <Property ID="66591455">
      <Profile ID="000000">\Acct_38058226\Prop_66591454\Prof_000000.xml</Profile>
    </Property>
  </Account>
  <Account ID="38058227">
    <Property ID="66591454">
      <Profile ID="000000">\Acct_38058226\Prop_66591454\Prof_000000.xml</Profile>
    </Property>
  </Account>
</Master>

Muenchian Grouping(<xsl:key>andの使用によって証明されるgenerate-id())が実際に進むべき道であることに注意してください。

于 2013-03-26T16:02:39.373 に答える