XSLT删除名称空间前缀但使用JDK XSLTC保留名称空间

时间:2014-02-11 11:37:58

标签: xml xslt namespaces

我需要从某个xml中删除命名空间前缀,但是使用XSLT保留命名空间。

这完全如此处所述 - How to remove namespace prefix leaving namespace value (XSLT)?

当我使用XML副本编辑器进行测试但我正在使用的应用程序正在使用XSLTC时,这很好用。不幸的是它不适用于XSLTC。关于如何使其发挥作用的任何想法?

这是源xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:eBooking xmlns:ns0="http://test.com">
  <ns0:eventInfo Id="ID00000000000000000020" version="1">
    <ns0:Event>
      <ns0:tpAmb>1</ns0:tpAmb>
      <ns0:procEmi>1</ns0:procEmi>
    </ns0:Event>
  </ns0:eventInfo>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
      <Reference URI="#ID00000000000000000020">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
          <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
        <DigestValue>p42saeKIMnVR7/P/nzen8mTsq94=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>SzLt....==</SignatureValue>
    <KeyInfo>
      <X509Data>
        <X509Certificate>MIIHdzCCBV.......==</X509Certificate>
      </X509Data>
    </KeyInfo>
  </Signature>
</ns0:eBooking>

这是xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns0="http://test.com"
                xmlns="http://test.com"  >
    <xsl:output indent="yes"/>
    <xsl:template match="ns0:*">
        <xsl:element name="{local-name()}" >
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

使用JDK XSLTC时,这会给出:

<?xml version="1.0" encoding="UTF-8"?>
<eBooking>
  <eventInfo Id="ID00000000000000000020" version="1">
    <Event>
      <tpAmb>1</tpAmb>
      <procEmi>1</procEmi>
    </Event>
  </eventInfo>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#" **xmlns:ns0="http://test.com"**>
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
      <Reference URI="#ID00000000000000000020">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
          <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
        <DigestValue>p42saeKIMnVR7/P/nzen8mTsq94=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>SzLt....==</SignatureValue>
    <KeyInfo>
      <X509Data>
        <X509Certificate>MIIHdzCCBV.......==</X509Certificate>
      </X509Data>
    </KeyInfo>
  </Signature>
</eBooking>

如果我使用xml拷贝编辑器进行测试,它可以正常工作。

1 个答案:

答案 0 :(得分:0)

这看起来像你在那里使用的XSLTC版本中的一个错误,应该使用样式表中声明的默认命名空间。你可以用

解决这个问题
    <xsl:element name="{local-name()}" namespace="{namespace-uri()}" >

对于您在xmlns:ns0元素上突出显示的Signature,这实际上是正确的行为,因为xsl:copy在复制时复制所有命名空间节点一个元素。要摆脱这个,你需要另一个模板

<!-- higher priority than node() but lower than ns0:* -->
<xsl:template match="*" priority="-0.4">
  <xsl:element name="{name()}" namespace="{namespace-uri()}" >
    <xsl:apply-templates select="@*|node()" />
  </xsl:element>
</xsl:template>