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>

我正在使用 XSLT 1.0。xml 每天晚上都在不断扩展。我对 xml/xslt 很陌生。我花了几个小时研究它,但交付非常接近,因此寻求帮助。我得到的提示是我可能必须使用 Muenchian 分组,形成键和分组。我还在学习,还不能为自己创造逻辑。提前致谢!!

4

1 回答 1

2

这是一种方法。

当这个 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 分组(如使用<xsl:key>and所证明的那样generate-id())确实是要走的路。

于 2013-03-26T16:02:39.373 回答