1

我有一个简单的 XML,我使用 XPATH 查询对 XML 进行了签名,例如 //*[@isDigSignReqd = 'true']。现在,签名的 XML 包含如下属性

xmlns="http://www.xyze.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

连接到每个节点。XML 签名验证发生得很好。但是,我可以删除这些属性吗?我对 XML 签名和所有这些都很陌生。请帮忙。

这就是 XML 的样子(部分)


<?xml version="1.0" encoding="UTF-8"?><XService xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xyzbe.org/xservice ACPDTLRequest.xsd">
 <request xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <header xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <BANK_ID isDigSignReqd="true" xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">DBS</BANK_ID>
   <LANGUAGE_ID isDigSignReqd="true" xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">001</LANGUAGE_ID>
   <CHANNEL_ID isDigSignReqd="true" xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">I</CHANNEL_ID>
   <LOGIN_FLAG isDigSignReqd="true" xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2</LOGIN_FLAG>

变换是这样创建的。

final XPathFilter2ParameterSpec xp2Spec = new XPathFilter2ParameterSpec(
Collections.singletonList(new XPathType("//*[@isDigSignReqd='true']", XPathType.Filter.INTERSECT)));
List<Transform> transforms = new ArrayList<Transform>() {
    private static final long serialVersionUID = 1L;
         {
    add(sigFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null));
    add(sigFactory.newTransform(Transform.XPATH2, xp2Spec ));
        } };

4

1 回答 1

0

现在,签名的 XML 包含如下属性

xmlns="http://www.xyze.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

连接到每个节点。XML 签名验证发生得很好。但是,我可以删除这些属性吗?

是的,提供的 XML 文档等价于:

<XService xsi:schemaLocation="http://www.xyzbe.org/xservice ACPDTLRequest.xsd"
          xmlns="http://www.xyzbe.org/xservice" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <request>
    <header>
      <BANK_ID isDigSignReqd="true">DBS</BANK_ID>
      <LANGUAGE_ID isDigSignReqd="true">001</LANGUAGE_ID>
      <CHANNEL_ID isDigSignReqd="true">I</CHANNEL_ID>
      <LOGIN_FLAG isDigSignReqd="true">2</LOGIN_FLAG>
    </header>
  </request>
</XService>

说明

默认命名空间对后代元素的所有名称都有效,不需要在它们上指定。

如何摆脱不必要的命名空间节点或声明

这是一个简单的 XSLT 解决方案,使用身份转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

当此转换应用于提供的 XML 文档时(更正为格式正确):

<XService xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xyzbe.org/xservice ACPDTLRequest.xsd">
    <request xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <header xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <BANK_ID isDigSignReqd="true" xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">DBS</BANK_ID>
            <LANGUAGE_ID isDigSignReqd="true" xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">001</LANGUAGE_ID>
            <CHANNEL_ID isDigSignReqd="true" xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">I</CHANNEL_ID>
            <LOGIN_FLAG isDigSignReqd="true" xmlns="http://www.xyzbe.org/xservice" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2</LOGIN_FLAG>
        </header>
    </request>
</XService>

结果是

<XService xsi:schemaLocation="http://www.xyzbe.org/xservice ACPDTLRequest.xsd"
          xmlns="http://www.xyzbe.org/xservice" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <request>
    <header>
      <BANK_ID isDigSignReqd="true">DBS</BANK_ID>
      <LANGUAGE_ID isDigSignReqd="true">001</LANGUAGE_ID>
      <CHANNEL_ID isDigSignReqd="true">I</CHANNEL_ID>
      <LOGIN_FLAG isDigSignReqd="true">2</LOGIN_FLAG>
    </header>
  </request>
</XService>
于 2011-02-28T17:04:26.110 回答