Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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/8/xslt/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
Xml 复制指定的元素、子元素和属性_Xml_Xslt - Fatal编程技术网

Xml 复制指定的元素、子元素和属性

Xml 复制指定的元素、子元素和属性,xml,xslt,Xml,Xslt,给定以下XML: <Products> <Batteries> <Name Value="Triple A"/> <Colour Value="Red"/> </Batteries> <Cups> <Name Value="Mugs"/> <Colour Value="Blue"/> <Log

给定以下XML:

<Products>
    <Batteries>
        <Name Value="Triple A"/>
        <Colour Value="Red"/>
    </Batteries>
    <Cups>
        <Name Value="Mugs"/>
        <Colour Value="Blue"/>
        <Logo>
            <Company Value="Super Clean!"/>
            <Colour Value="Red"/>
        </Logo>
    </Cups>
    <Cups>
        <Name Value="Teacups"/>
        <Colour Value="Orange"/>
        <Handle Value="Dainty"/>
        <Logo>
            <Company Value="Lovely teas"/>
            <Colour Value="Red"/>
        </Logo>
    </Cups>
</Products>
经过一番周旋,我得到了这个:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:template>
    <xsl:template match="/">
        <Products>
            <xsl:apply-templates/>
        </Products>
    </xsl:template>
    <xsl:template match="Cups|Cups//*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

但它并不是跨属性复制到输出


注意:这不是我的XML,也没有它的模式,但我确信会有
Cups

问题在于这个模板

<xsl:template match="@*|node()">
    <xsl:apply-templates select="@*|node()"/>
</xsl:template>
但是,看起来您真正想要做的是实际删除
电池
节点及其子节点。如果是这样,请使用标识模板复制其他所有内容,只需使用一个模板来忽略电池,就像这样

试试这个XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Batteries" />
</xsl:stylesheet>

编辑:对于您的评论,如果您希望复制的元素是少数,请尝试此方法

<xsl:template match="Cups">
    <xsl:copy-of select="." />
</xsl:template>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="@*|node()" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Products/*" />

    <xsl:template match="Cups|Spoons" priority="2">
        <xsl:call-template name="identity" />
    </xsl:template>
</xsl:stylesheet>


这将删除
Products
下的所有元素,除了
Cups
Spoons

您更新且非常正确的
Cups
匹配模板完成了此工作!我真的不能用一个模板来移除电池,因为我精心设计的示例并没有告诉我,在我的实际XML中,还有很多不同的元素,我想复制的元素只是少数,但是你的答案完全符合要求。非常感谢!责怪自己没有意识到这一点:)如果有帮助的话,我已经扩展了我的答案,展示了一种稍微不同的方法,用于你希望复制的元素处于少数的情况。这非常有用,我希望我可以多次投票!
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="@*|node()" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Products/*" />

    <xsl:template match="Cups|Spoons" priority="2">
        <xsl:call-template name="identity" />
    </xsl:template>
</xsl:stylesheet>