我想通过将每个父级 PurchaseHistory 的属性 Type 复制到其子级 StoreTransaction
来转换下面的 xml输入Xml:
<ArrayOfStoreTransaction CustomerID="C1">
<PurchaseHistory Type="purchase.001" Count="2">
<StoreTransaction>
<CustomerID>C1</CustomerID>
<CustomerAccount>C1A</CustomerAccount>
</StoreTransaction>
<StoreTransaction>
<CustomerID>C1</CustomerID>
<CustomerAccount>C1A</CustomerAccount>
</StoreTransaction>
</PurchaseHistory>
<PurchaseHistory Type="purchase.002" Count="1">
<StoreTransaction>
<CustomerID>S3</CustomerID>
<CustomerAccount>S3A</CustomerAccount>
</StoreTransaction>
</PurchaseHistory>
<PurchaseHistory Type="purchase.003" Count="2">
<StoreTransaction>
<CustomerID>S3</CustomerID>
<CustomerAccount>S3A</CustomerAccount>
</StoreTransaction>
<StoreTransaction>
<CustomerID>S3</CustomerID>
<CustomerAccount>S3A</CustomerAccount>
</StoreTransaction>
</PurchaseHistory>
</ArrayOfStoreTransaction>
输出XML:
<ArrayOfStoreTransaction CustomerID="C1">
<StoreTransaction Type="purchase.001">
<CustomerID>C1</CustomerID>
<CustomerAccount>C1A</CustomerAccount>
</StoreTransaction>
<StoreTransaction Type="purchase.001">
<CustomerID>C1</CustomerID>
<CustomerAccount>C1A</CustomerAccount>
</StoreTransaction>
<StoreTransaction Type="purchase.002">
<CustomerID>S3</CustomerID>
<CustomerAccount>S3A</CustomerAccount>
</StoreTransaction>
<StoreTransaction Type="purchase.003">
<CustomerID>S3</CustomerID>
<CustomerAccount>S3A</CustomerAccount>
</StoreTransaction>
<StoreTransaction Type="purchase.003">
<CustomerID>S3</CustomerID>
<CustomerAccount>S3A</CustomerAccount>
</StoreTransaction>
</ArrayOfStoreTransaction>
所以我想要一个好的java库来以有效的方式转换我的xml,考虑到内存和执行时间,因为xml将非常大> 200mb
您只需要两个简单的 XSLT 模板规则即可:
<xsl:template match="PurchaseHistory">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="StoreTransaction">
<StoreTransaction Type="{../@Type}">
<xsl:apply-templates/>
</StoreTransaction>
</xsl:template>
一个好的现代 XSLT 转换引擎应该能够毫无困难地处理 200Mb。如果它进入千兆字节范围,并且您使用支持 XSLT 3.0 流式传输的引擎,则此转换是完全可流式传输的。
不要浪费时间用 Java 进行编码。