Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用XLST重命名父XML节点和子XML节点?_Xml_Xslt - Fatal编程技术网

如何使用XLST重命名父XML节点和子XML节点?

如何使用XLST重命名父XML节点和子XML节点?,xml,xslt,Xml,Xslt,我试图重命名以下位于XML文档中几层的父/子注释 <product-lineitem> <price-adjustments> <price-adjustment> ... </price-adjustment> <price-adjustment> ... </price-adjustment> &l

我试图重命名以下位于XML文档中几层的父/子注释

<product-lineitem>
    <price-adjustments>
        <price-adjustment>
           ...
        </price-adjustment>
        <price-adjustment>
           ...
        </price-adjustment>
    </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转换代码,因为我还在学习

不,您没有创建代码气味。您正在使用的模式、标识模板以及要更改的元素的覆盖模板通常是一种方法

可以简化的一点是,实际上不需要指定匹配元素的完整路径。只要元素名就行了

<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="price-adjustments">
        <line-price-adjustments>
            <xsl:apply-templates select="@*|node()" />
        </line-price-adjustments>
    </xsl:template>

    <xsl:template match="price-adjustment">
        <line-price-adjustment>
            <xsl:apply-templates select="@*|node()" />
        </line-price-adjustment>
    </xsl:template>
</xsl:transform>

例如,如果在不同的元素名称下有一个
价格调整
,您只需要指定一个更完整的路径,而您不想更改它


如果您确信要匹配的元素上永远不会有属性,则还可以将
替换为

如果您只是想收紧代码,也可以使用以下模板

    <xsl:template match="price-adjustment | price-adjustments">
        <xsl:element name="line-{name()}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

。。。或:

    <xsl:template match="*[starts-with(name(), 'price-adjustment')]">
        <xsl:element name="line-{name()}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

在示例输入XML的特定情况下,像这样缩短代码没有多大用处。但是,如果您有许多元素想要以类似的方式重命名,只需在前面加上一个字符串或附加另一个字符串,就可以避免编写大量基本相同的模板。

可以简化为
    <xsl:template match="*[starts-with(name(), 'price-adjustment')]">
        <xsl:element name="line-{name()}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>