【发布时间】:2017-07-13 15:41:34
【问题描述】:
我正在尝试重命名以下位于 XML 文档中的几个级别的父/子注释
<product-lineitem>
<price-adjustments>
<price-adjustment>
...
</price-adjustment>
<price-adjustment>
...
</price-adjustment>
</price-adjustments>
</product-lineitem>
进入
<product-lineitem>
<line-price-adjustments>
<line-price-adjustment>
...
</line-price-adjustment>
<line-price-adjustment>
...
</line-price-adjustment>
</line-price-adjustments>
</product-lineitem>
我已经想出了如何使用 XSLT 做到这一点,但我认为我在重复我的逻辑并且可能滥用 xslt,是否可以在少于以下两个模板中进行上述转换
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="product-lineitem/price-adjustments">
<line-price-adjustments><xsl:apply-templates select="@*|node()" /></line-price-adjustments>
</xsl:template>
<xsl:template match="product-lineitem/price-adjustments/price-adjustment">
<line-price-adjustment><xsl:apply-templates select="@*|node()" /> </line-price-adjustment>
</xsl:template>
</xsl:transform>
我想我正在创造 xml 转换代码的味道,因为我还在学习!
【问题讨论】: