You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSLT删除XML指定属性与元素失败,请求技术协助

问题描述

需要修改XML文件,删除指定元素和属性,但当前XSLT无法选中目标元素。

原始XML

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xml:base="https://urldebas.com">
    <id>https://urldebas.com</id>
    <title type="text">value</title>
    <updated>2022-10-12T17:11:09Z</updated>
    <author>
        <name/>
    </author>
    <link href="EQUIPMENTSet" rel="self" title="EQUIPMENTSet"/>
    <entry>
        <id>https://urldebas.com</id>
        <title type="text">EQUIPMENTSet(Plant='1268',EquipmentID='10042621')</title>
        <updated>2022-10-12T17:11:09Z</updated>
        <category term="ZPM_MASTER_DATA_SRV.EQUIPMENT" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
        <link href="EQUIPMENTSet(Plant='1268',EquipmentID='10042621')" rel="self" title="EQUIPMENT"/>
        <content type="application/xml">
            <m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
                <d:Plant>1268</d:Plant>
                <d:EquipmentID>10042621</d:EquipmentID>
                <d:DateOfLastChange>2021-05-03T00:00:00</d:DateOfLastChange>
                <d:Description>AEROTHERME No1</d:Description>
                <d:FunctionalLocation>FR29-1268-BA70-AERG</d:FunctionalLocation>
                <d:MainWorkCenter>PM1268SG</d:MainWorkCenter>
                <d:CostCenter>BE01G001</d:CostCenter>
                <d:SortField>AEROTHERME 1</d:SortField>
                <d:ObjectType></d:ObjectType>
                <d:WorkCenter></d:WorkCenter>
            </m:properties>
        </content>
    </entry>
</feed>

需求

  • 删除feed元素中的xml:base属性
  • 删除feed和entry下的id元素

预期输出XML

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<title type="text">value</title>
<updated>2022-10-12T17:11:09Z</updated>
<author>
    <name />
</author>
<link href="EQUIPMENTSet" rel="self" title="EQUIPMENTSet" />
<entry>
    <title type="text">EQUIPMENTSet(Plant='1268',EquipmentID='10042621')</title>
    <updated>2022-10-12T17:11:09Z</updated>
    <category term="ZPM_MASTER_DATA_SRV.EQUIPMENT" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link href="EQUIPMENTSet(Plant='1268',EquipmentID='10042621')" rel="self" title="EQUIPMENT" />
    <content type="application/xml">
        <m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
            <d:Plant>1268</d:Plant>
            <d:EquipmentID>10042621</d:EquipmentID>
            <d:DateOfLastChange>2021-05-03T00:00:00</d:DateOfLastChange>
            <d:Description>AEROTHERME No1</d:Description>
            <d:FunctionalLocation>FR29-1268-BA70-AERG</d:FunctionalLocation>
            <d:MainWorkCenter>PM1268SG</d:MainWorkCenter>
            <d:CostCenter>BE01G001</d:CostCenter>
            <d:SortField>AEROTHERME 1</d:SortField>
            <d:ObjectType></d:ObjectType>
            <d:WorkCenter></d:WorkCenter>
        </m:properties>
    </content>
</entry>

当前无效的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy>
</xsl:template>
<xsl:template match="@xml:base"/>
<xsl:template match="feed/id"/>
<xsl:template match="feed/entry/id"/>
</xsl:stylesheet>

解决方案

问题根源在于XML的命名空间:原始XML中的feed、id、entry等元素都属于http://www.w3.org/2005/Atom命名空间,但原XSLT未声明该命名空间,导致模板无法匹配目标元素。

修改后的有效XSLT如下:

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:atom="http://www.w3.org/2005/Atom">
  <!-- 身份模板:默认复制所有节点和属性 -->
  <xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy>
  </xsl:template>

  <!-- 删除xml:base属性 -->
  <xsl:template match="@xml:base"/>

  <!-- 删除feed下的id元素 -->
  <xsl:template match="atom:feed/atom:id"/>

  <!-- 删除entry下的id元素 -->
  <xsl:template match="atom:feed/atom:entry/atom:id"/>
</xsl:stylesheet>

关键修改说明

  1. 声明命名空间:在xsl:stylesheet标签中添加xmlns:atom="http://www.w3.org/2005/Atom",为Atom命名空间指定前缀atom。
  2. 前缀匹配元素:所有匹配feed、id、entry的模板都需添加atom:前缀,确保匹配到命名空间下的目标元素。
  3. xml:base属性无需前缀:xml:是内置命名空间,直接match="@xml:base"即可生效。

内容的提问来源于stack exchange,提问作者Sidiki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 14:35:57