【问题标题】:Merge equal namespace prefixes with XSLT将相等的名称空间前缀与 XSLT 合并
【发布时间】:2014-03-06 14:28:27
【问题描述】:

假设您有一个定义了多个命名空间前缀的 XML 块,其中一些实际上是相同的命名空间,只是前缀不同。使用 XSLT 是否有一种不太复杂的方法来合并这些前缀,以便最终每个名称空间只有一个前缀?比如选最短的?


示例

<soapenv:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:f="http://api.example.com/Service"
    xmlns:foo="http://api.example.com/Service">

   <soapenv:Body>

      <foo:serviceResponse>
         <f:profile id="1">Alice</f:profile>
         <f:profile id="2">Bob</f:profile>
      </foo:serviceResponse>

   </soapenv:Body>
</soapenv:Envelope>

例如应该变成这样:

<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:f="http://api.example.com/Service">

   <soap:Body>

      <f:serviceResponse>
         <f:profile id="1">Alice</f:profile>
         <f:profile id="2">Bob</f:profile>
      </f:serviceResponse>

   </soap:Body>
</soap:Envelope>

【问题讨论】:

  • 只要命名空间正确,为什么还要关心前缀?
  • 因为我是一个喜欢干净的完美主义者。如果有一个简单的解决方案可以解决这个问题,我可以添加它而不必再为它烦恼;)
  • "如果有一个简单的解决方案" 好吧,现在你知道了,哈哈。

标签: xml xslt namespaces xslt-2.0


【解决方案1】:

你可以试试

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="namespaces" match="dec" use="@ns"/>

<xsl:variable name="prefs">
  <xsl:for-each-group select="/*/namespace::*" group-by="string()">
    <xsl:for-each select="current-group()">
      <xsl:sort select="string-length(local-name())"/>
      <xsl:if test="position() eq 1">
        <dec ns="{string()}"><xsl:value-of select="local-name()"/></dec>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each-group>
</xsl:variable>

<xsl:template match="@*">
  <xsl:attribute name="{if (key('namespaces', namespace-uri(), $prefs)) then concat(key('namespaces', namespace-uri(), $prefs), ':') else ''}{local-name()}" namespace="{namespace-uri()}" select="."/>
</xsl:template>

<xsl:template match="*">
  <xsl:element name="{if (key('namespaces', namespace-uri(), $prefs)) then concat(key('namespaces', namespace-uri(), $prefs), ':') else ''}{local-name()}" namespace="{namespace-uri()}">
    <xsl:apply-templates select="@* , node()"/>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>

但这仅检查根元素上的命名空间声明(虽然它们允许在文档中的任何元素上使用),更重要的是,XML 模式或 SOAP 消息中的属性值可以是 QName 类型(例如 xs:integer)和这样生成的文档可能不再有效(例如,您声明了两个前缀 xxs,算法消除了 xs,但属性值不固定为 x:integer)。因此,如果您想使用该 XSLT 来修复可能使用限定名称作为元素或属性值的 SOAP/XML 模式内容(而不仅仅是 XSLT 修复/简化的元素或属性名称),请注意。

【讨论】:

    【解决方案2】:

    您对此没有太多控制权 - 这主要取决于您的处理器的突发奇想。例如,将恒等转换模板应用于您的输入将导致:

    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:f="http://api.example.com/Service" xmlns:foo="http://api.example.com/Service">
      <soapenv:Body>
        <foo:serviceResponse>
          <foo:profile id="1">Alice</foo:profile>
          <foo:profile id="2">Bob</foo:profile>
        </foo:serviceResponse>
      </soapenv:Body>
    </soapenv:Envelope>
    

    使用 libxslt 时,但 Saxon 会返回:

    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:f="http://api.example.com/Service" xmlns:foo="http://api.example.com/Service">
       <soapenv:Body>
          <foo:serviceResponse>
             <f:profile id="1">Alice</f:profile>
             <f:profile id="2">Bob</f:profile>
          </foo:serviceResponse>
       </soapenv:Body>
    </soapenv:Envelope>
    

    您可以尝试完全去除前缀,使用类似:

    <xsl:template match="*">
        <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    

    在这里,撒克逊人将回归:

    <?xml version="1.0" encoding="UTF-8"?>
    <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
       <Body>
          <serviceResponse xmlns="http://api.example.com/Service">
             <profile id="1">Alice</profile>
             <profile id="2">Bob</profile>
          </serviceResponse>
       </Body>
    </Envelope>
    

    而 libxslt 会生成一些随机(但统一)的前缀,例如

    <?xml version="1.0" encoding="UTF-8"?>
    <ns13:Envelope xmlns:ns13="http://schemas.xmlsoap.org/soap/envelope/">
      <ns13:Body>
        <ns14:serviceResponse xmlns:ns14="http://api.example.com/Service">
          <ns14:profile id="1">Alice</ns14:profile>
          <ns14:profile id="2">Bob</ns14:profile>
        </ns14:serviceResponse>
      </ns13:Body>
    </ns13:Envelope>
    

    他们都没有错。

    【讨论】:

    • 对于 XSLT 2.0 和 XSLT 2.0 处理器,序列化不应依赖于处理器的突发奇想。
    【解决方案3】:

    有趣的问题。这接近你需要的。不过没有花太多时间,所以可能有办法大大缩短它。

    • 较短的前缀用于输出 XML
    • 元素上有多余的命名空间声明。这是因为它在运行时决定哪些前缀应该被丢弃,并且您只能将 NCNames 的静态列表指定为 exclude-result-prefixes 的值。

    后者可以在下面的转换之后修复,通过简单的恒等转换来排除不再使用的前缀。例如,参见 Dimitre Novatchev 的回答 here

    样式表

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:f="http://api.example.com/Service"
        xmlns:foo="http://api.example.com/Service">
    
        <xsl:output method="xml" indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="*">
          <xsl:variable name="node" select="."/>
          <xsl:variable name="prefix" select="substring-before(name(), concat(':', local-name()))"/>
          <xsl:variable name="scope">
              <xsl:for-each select="in-scope-prefixes(.)">
                  <xsl:element name="{.}">
                      <xsl:value-of select="namespace-uri-for-prefix(.,$node)"/>
                  </xsl:element>
              </xsl:for-each>
          </xsl:variable>
          <xsl:choose>
              <xsl:when test="$scope/*[name() != $prefix and namespace-uri-for-prefix($prefix,$node) = . and string-length(./name()) lt string-length($prefix)]">
                  <xsl:variable name="match" select="$scope/*[name() != $prefix and namespace-uri-for-prefix($prefix,$node) = .]"/>
                  <xsl:element name="{concat($match/name(),':',$node/local-name())}">
                      <xsl:apply-templates select="@*|node()"/>
                  </xsl:element>
              </xsl:when>
              <xsl:otherwise>
                    <xsl:copy>
                       <xsl:apply-templates select="@*|node()"/>
                    </xsl:copy>
              </xsl:otherwise>
          </xsl:choose>
    
        </xsl:template>
    
        <xsl:template match="@*|processing-instruction()|text()|comment()">
            <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    输出

    <?xml version="1.0" encoding="UTF-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <soap:Body>
          <f:serviceResponse xmlns:f="http://api.example.com/Service">
             <f:profile xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                        xmlns:foo="http://api.example.com/Service"
                        id="1">Alice</f:profile>
             <f:profile xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                        xmlns:foo="http://api.example.com/Service"
                        id="2">Bob</f:profile>
          </f:serviceResponse>
       </soap:Body>
    </soap:Envelope>
    

    输出(应用类似于this 的内容后)

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <soap:Body>
          <f:serviceResponse xmlns:f="http://api.example.com/Service">
             <f:profile id="1">Alice</f:profile>
             <f:profile id="2">Bob</f:profile>
          </f:serviceResponse>
       </soap:Body>
    </soap:Envelope>
    

    【讨论】:

      【解决方案4】:

      这就是我最终使用的。不知道它与其他答案相比如何,或者它是否具有相同的功能集,但它适用于我的情况,我不记得我是如何得到它的了。如果我在这里调整了其中一个答案,或者我在其他地方找到了答案。无论哪种情况,它都对我有用。如果其他人有任何遗漏或更好的地方,请发表评论并点赞:)

      <stylesheet version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform">
      
          <!--
              Pulls namespace declarations up towards root node.
          -->
      
          <template match="@* | text() | processing-instruction() | comment()">
              <copy />
          </template>
      
          <template match="*">
              <copy copy-namespaces="no">
                  <for-each-group group-by="local-name()" select="descendant-or-self::*/namespace::*">
                      <copy-of select="." />
                  </for-each-group>
                  <apply-templates select="@* , node()"/>
              </copy>
          </template>
      
      </stylesheet>
      

      注意:不要认为这实际上是合并前缀(不记得了,暂时无法测试),但我最终使用了它在返回 SOAP 消息和命名空间之前清理了很多 ESB 进程,因此它至少确实做了一些事情:p

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-30
        • 1970-01-01
        • 2011-07-03
        • 1970-01-01
        • 2018-10-11
        相关资源
        最近更新 更多