1

我可以根据我的要求使用以下 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 将包含对命名空间的引用作为每个元素的前缀。如每个元素的命名空间前缀“ns2”下方的 xml 中所示。下面是带有“ns2”命名空间前缀的 xml。在这种情况下,我的 xslt 失败并且无法执行转换。谁能帮我理解如何在 xslt 中处理这个命名空间问题,以便带有和不带命名空间前缀的 xml 可以通过相同的 xslt 进行转换?

<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>

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

问候, 拉胡尔

4

1 回答 1

0

经过这个论坛的所有讨论,我的问题已经解决了。下面是更正后的 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>
于 2013-08-15T11:08:55.310 回答