我开始学习xslt来操作xml并尝试压缩具有许多不同子节点和孙子节点的xml。是否有任何简单的xslt来转换输入xml以生成output.xml。
input.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输出。
output.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>
答案 0 :(得分: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>
等