xslt - 在转换 xml 时处理命名空间问题

标签 xslt namespaces

我能够根据我的要求使用以下 xml 作为输入来转换 xml。

<message
    xmlns="http://www.origoservices.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
  <m_control>
    <control_timestamp>2013-06-06T14:55:37</control_timestamp>
    <initiator_id>ASL</initiator_id>
  </m_control>
  <m_content>
    <b_control>
      <quote_type>Single Company</quote_type>
      <quote_or_print>Quote And Print</quote_or_print>
      <generic_quote_ind>Yes</generic_quote_ind>
      <tpsdata>
        <tps_quote_type>Comparison</tps_quote_type>
      </tpsdata>
    </b_control>
    <application>
      <product>
        <tpsdata>
          <service_type>QuickQuote</service_type>
          <quote_type>Standard</quote_type>
        </tpsdata>
      </product>
    </application>
  </m_content>
</message>

但问题是,有时输入 XML 会包含对 Namespace 的引用作为每个元素的前缀。如每个元素的命名空间前缀“ns2”下面的 xml 所示。下面是带有“ns2”命名空间前缀的 xml。在这种情况下,我的 xslt 失败并且无法执行转换。任何人都可以帮助我理解如何在 xslt 中处理这个命名空间问题,以便可以通过同一个 xslt 转换带有和不带有命名空间前缀的 xml?
<ns2:message xmlns:ns2="http://www.origoservices.com"
  xmlns="http://www.w3.org/2000/09/xmldsig#"
>
  <ns2:m_control>
    <ns2:control_timestamp>2013-06-06T14:55:37</ns2:control_timestamp>
    <ns2:initiator_id>ASL</ns2:initiator_id>
  </ns2:m_control>
  <ns2:m_content>
    <ns2:b_control>
      <ns2:quote_type>Single Company</ns2:quote_type>
      <ns2:quote_or_print>Quote And Print</ns2:quote_or_print>
      <ns2:generic_quote_ind>Yes</ns2:generic_quote_ind>
      <ns2:quote_response_status>Error</ns2:quote_response_status>
<ns2:tpsdata>
<ns2:tps_quote_type
xmlns="http://www.origoservices.com"
xmlns:ns2="http://www.w3.org/2000/09/xmldsig#"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>Comparison</ns2:tps_quote_type>
</ns2:tpsdata>


    </ns2:b_control>
    <ns2:application>
      <ns2:product>
        <ns2:tpsdata>
          <ns2:service_type>QuickQuote</ns2:service_type>
          <ns2:quote_type>Standard</ns2:quote_type>
        </ns2:tpsdata>
      </ns2:product>
    </ns2:application>
  </ns2:m_content>
</ns2:message>

我在 xslt 下面使用。
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:o="http://www.origoservices.com"
    xmlns:dp="http://www.datapower.com/extensions"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:date="http://exslt.org/dates-and-times"
    extension-element-prefixes="dp"
    exclude-result-prefixes="fn date">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="o:b_control/o:quote_type[../o:tpsdata/o:tps_quote_type = 'Comparison']">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:text>Comparison</xsl:text>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="o:tpsdata[o:tps_quote_type = 'Comparison']" />
</xsl:stylesheet>

如果需要更多信息,请告诉我。

问候,
拉胡尔

最佳答案

在此论坛上进行了所有讨论后,我的问题已解决。下面是更正的 xslt 来处理我面临的命名空间问题。

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:o="http://www.origoservices.com" xmlns:dp="http://www.datapower.com/extensions" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:date="http://exslt.org/dates-and-times" xmlns:g="http://www.w3.org/2000/09/xmldsig#" version="1.0" extension-element-prefixes="dp" exclude-result-prefixes="fn date">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node() | @*">
<xsl:copy>
    <xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="o:b_control/o:quote_type[../o:tpsdata/g:tps_quote_type = 'Comparison']">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:text>Comparison</xsl:text>
</xsl:copy>
</xsl:template>
<xsl:template match="o:tpsdata[g:tps_quote_type = 'Comparison']"/>
<xsl:template match="o:b_control/o:quote_type[../o:tpsdata/o:tps_quote_type = 'Comparison']">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:text>Comparison</xsl:text>
</xsl:copy>
</xsl:template>
<xsl:template match="o:tpsdata[o:tps_quote_type = 'Comparison']"/>
</xsl:stylesheet>

关于xslt - 在转换 xml 时处理命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18189255/

相关文章:

xml - XML/XSL:xpath和条件(否则)

C# XSLT 快速转换大型 XML 文件

xslt - 从 xslt 在 href 中传递多个参数

xml - XPath - 测试是否至少有一个节点具有给定值

XML 命名空间 - 默认声明

php - 使用 Thrift 生成的文件中的 PSR-4 命名空间

xslt - 通过 XSLT 替换 XHTML 中的 style= 属性时的模糊规则匹配

python - 是否有等效于 Perls 'package' 关键字的 Python

excel - 在 VBA 中引用行名称

javascript - 在 JavaScript 中是否有 "concise"命名空间的方法?