-1

我开始学习 xslt 来操作 xml 并尝试扁平化具有许多不同子节点和孙节点的 xml。是否有任何简单的 xslt 可以将输入 xml 转换为 output.xml。

输入.xml:

<Addresses>
  <Address__c>
    <Id>a0H20000002uhsyEAA</Id>
    <R00N20000001XA10EAG>
        <Customer_Installation_Address__c>
            <Id>a0L2000000DDpOCEA1</Id>
        </Customer_Installation_Address__c>
        <Customer_Installation_Address__c>
            <Id>a0L2000000DDpOCEB2</Id>
        </Customer_Installation_Address__c>
    </R00N20000001XA10EAG>
    <Customers__r>
        <Account>
            <Id>001M000000FOEiHIAX</Id>
        </Account>
        <Account>
            <Id>001M000000FOAAbIAP</Id>
        </Account>
    </Customers__r>
    <Cases__r>
        <Case>
            <Id>500D000000G7drpIAB</Id>
        </Case>
    </Cases__r>
   </Address__c>
   <Address__c>
    <Id>a0H20000003JjyzEAC</Id>
    <Cases__r>
        <Case>
            <Id>500D000000G7drpIAB</Id>
        </Case>
    </Cases__r>
   </Address__c>
   <Address__c>
    <Id>a0HD0000007wQ0EMAU</Id>
    <Customers__r>
        <Account>
            <Id>001M000000FOEiHIAX</Id>
        </Account>
        <Account>
            <Id>001M000000FOAAbIAP</Id>
        </Account>
    </Customers__r>
   </Address__c>
</Addresses>

并希望产生以下 xml 输出。

输出.xml

<Addresses>
  <Address__c>
    <AddrId>a0H20000002uhsyEAA</AddrId>
    <CIAddressId>a0L2000000DDpOCEA1</CIAddressId>
    <CIAddressId>a0L2000000DDpOCEB2</CIAddressId>
    <AccountId>001M000000FOEiHIAX</AccountId>
    <AccountId>001M000000FOAAbIAP</AccountId>
    <CaseId>500D000000G7drpIAB</CaseId>
  </Address__c>
  <Address__c>
    <AddrId>a0H20000003JjyzEAC</AddrId>
    <CaseId>500D000000G7drpIAB</CaseId>
  </Address__c>
  <Address__c>
    <AddrId>a0HD0000007wQ0EMAU</AddrId>
    <AccountId>001M000000FOEiHIAX</AccountId>
    <AccountId>001M000000FOAAbIAP</AccountId>
   </Address__c>
</Addresses>
4

1 回答 1

1

像这样的东西(有更简洁的表达方式):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <Addresses>
      <xsl:apply-templates select="Addresses/Address__c" />
    </Addresses>
  </xsl:template>

  <xsl:template match="Address__c">
    <Address__c>
      <xsl:apply-templates select="descendant::Id" />
    </Address__c>
  </xsl:template>

  <xsl:template match="Id">
    <Id><xsl:value-of select="." /></Id>
  </xsl:template>
</xsl:stylesheet>

会给你

<Addresses>
<Address__c>
    <Id>a0H20000002uhsyEAA</Id>
    <Id>a0L2000000DDpOCEA1</Id>
    <Id>a0L2000000DDpOCEB2</Id>
    <!-- ... -->

Id现在您可以添加额外的模板来根据其父元素更改元素的名称:

<xsl:template match="Address__c/Id">
  <AddrId><xsl:value-of select="." /></AddrId>
</xsl:template>

<xsl:template match="Customer_Installation_Address__c/Id">
  <CIAddressId><xsl:value-of select="." /></CIAddressId>
</xsl:template>

等等

于 2012-08-10T13:50:17.993 回答