1

下面是我的要求。

1)我希望根据评估为真的特定条件添加一个元素“错误”作为最后一个元素。2)如果评估为真,我希望根据其他条件在“quote_or_print”元素之后添加一个元素“generic_quote_ind”。

下面是我编写的输入 XML、XSLT 和预期的输出 XML。

使用这段代码,我可以添加 'error' 元素作为最后一个元素,但我无法在 'quote_or_print' 元素之后添加 'generic_quote_ind'。

请指导我如何实现这一目标?

输入 XML

 <?xml version="1.0" encoding="utf-8"?>
<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>Comparison</quote_type>
        <quote_or_print>Quote And Print</quote_or_print>
        <message_version_number>3.7</message_version_number>
</b_control>
  <application>
    <product>
        <tpsdata>
            <service_type>QuickQuote</service_type>
            <quote_type>Standard</quote_type>
        </tpsdata>
    </product>
</application>
</m_content>
</message>

预期产出

<?xml version="1.0" encoding="UTF-8"?>
<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>Comparison</quote_type>
        <quote_or_print>Quote And Print</quote_or_print>
        <generic_quote_ind>Yes</generic_quote_ind>
        <message_version_number>3.7</message_version_number>
    </b_control>
<application>
    <product>
        <tpsdata>
            <service_type>QuickQuote</service_type>
            <quote_type>Standard</quote_type>
        </tpsdata>
    </product>
</application>
</m_content>
</message>

我尝试过的 XSLT

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    xmlns:dp="http://www.datapower.com/extensions" xmlns:fn="http://www.w3.org/2005/xpath- functions" xmlns:date="http://exslt.org/dates-and-times" version="1.0" extension-element-prefixes="dp">
 <xsl:output method="xml" indent="yes"/>
<xsl:template match="*">
<!-- identity with closing tags -->
<xsl:element name="{name()}">
    <xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:variable name="GenericQuoteInd">
<xsl:value-of select="/*[namespace-uri()='http://www.origoservices.com' and local- name()='message']/*[namespace-uri()='http://www.origoservices.com' and local-name()='m_content']/*[namespace-uri()='http://www.origoservices.com' and local-name()='b_control']/*[namespace-uri()='http://www.origoservices.com' and local-name()='generic_quote_ind']"/>
</xsl:variable>
<xsl:variable name="InitiatorId">
<xsl:value-of select="/*[namespace-uri()='http://www.origoservices.com' and local-name()='message']/*[namespace-uri()='http://www.origoservices.com' and local-name()='m_control']/*[namespace-uri()='http://www.origoservices.com' and local-name()='initiator_id']"/>
</xsl:variable>
<xsl:variable name="ServiceType">
<xsl:value-of select="/*[namespace-uri()='http://www.origoservices.com' and local-name()='message']/*[namespace-uri()='http://www.origoservices.com' and local-name()='m_content']/*[namespace-uri()='http://www.origoservices.com' and local-name()='application']/*[namespace-uri()='http://www.origoservices.com' and local-name()='product']/*[namespace-uri()='http://www.origoservices.com' and local-name()='tpsdata']/*[namespace-uri()='http://www.origoservices.com' and local-name()='service_type']"/>
</xsl:variable>
<xsl:variable name="quoteNPrint">
<xsl:value-of select="/*[namespace-uri()='http://www.origoservices.com' and local-name()='message']/*[namespace-uri()='http://www.origoservices.com' and local-name()='m_content']/*[namespace-uri()='http://www.origoservices.com' and local-name()='b_control']/*[namespace-uri()='http://www.origoservices.com' and local-name()='quote_or_print']"/>
</xsl:variable>
<xsl:template match="/*[namespace-uri()='http://www.origoservices.com' and local-name()='message']/*[namespace-uri()='http://www.origoservices.com' and local-name()='m_content']/*[namespace-uri()='http://www.origoservices.com' and local-name()='b_control']">
<xsl:choose>
<xsl:when test="(($GenericQuoteInd = 'Yes') or (($InitiatorId = 'ASL') and   ($ServiceType='QuickQuote'))) and ($quoteNPrint='Quote And Print')">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:element name="error" namespace="{namespace-uri()}">can not provide quotation</xsl:element>
    </xsl:copy>
</xsl:when>
<xsl:when test="(($GenericQuoteInd = '') and (($InitiatorId = 'ASL') and ($ServiceType='QuickQuote'))) and ($quoteNPrint='QuoteOnly')">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:element name="generic_quote_ind" namespace="{namespace-uri()}">Yes</xsl:element>
    </xsl:copy>
</xsl:when>
<xsl:when test="($GenericQuoteInd = 'Yes') and ($quoteNPrint='QuoteOnly')">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:when>
<!--xsl:when test="($GenericQuoteInd = ' Yes') or (($InitiatorId = 'ASL') and ($ServiceType='QuickQuote')) and ($quoteNPrint='QuoteOnly')">
<xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:when-->
<xsl:otherwise>
<xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    <xsl:element name="otherwise_loop" namespace="{namespace-uri()}">Yes</xsl:element>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*|comment()|processing-instruction()">
<xsl:copy>
    <xsl:copy-of select="@*|namespace::*"/>
    <xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
4

1 回答 1

1

尝试这个 ...

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

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

<xsl:template match="o:b_control[
    ((../../o:m_control/o:initiator_id='ASK') or (o:generic_quote_ind='Yes')) and
    (../o:application/o:tpsdata/o:service_type='QuickResponse') and
    (o:quote_or_print='Quote And Print')]">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()" />
  <error xmlns="http://www.origoservices.com">unable to provide quote</error> 
 </xsl:copy>
</xsl:template>

<xsl:template match="o:b_control[
    ((../../o:m_control/o:initiator_id='ASK') or (o:generic_quote_ind='Yes')) and
    (../o:application/o:tpsdata/o:service_type='QuickResponse') and
    (o:quote_or_print='Quote Only')]
    [not( o:generic_quote_ind)]">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()" />
  <generic_quote_ind xmlns="http://www.origoservices.com">Yes</generic_quote_ind> 
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>
于 2013-07-23T02:04:47.060 回答