繁体   English   中英

如果标签不存在,则使用XSLT将XML标签添加到SOAP消息中

[英]add XML tag to SOAP message with XSLT if tag doesn't exist

我对XSLT还是很陌生,不知道如何为所需的转换找到解决方案。 我们收到一个SOAP请求,需要检查是否存在某个标记(步骤)。 如果不存在,我想将其添加到SOAP消息中。

我们正在Azure API Management上运行该接口,该接口仅在版本1.0中允许XSLT。

XML范例

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
   <ProductionToSupply xmlns="http://somenamespace.com">
      <ProductionDataList>
         <ProductionData>
            <Material_Number>5</Material_Number>
            <Doc_Number>1234</Doc_Number>
            <Description>abcde</Description>
         </ProductionData>
      </ProductionDataList>
   </ProductionToSupply>
</soap:Body>
</soap:Envelope>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  
xmlns:ns1="http://somenamespace.com" exclude-result-prefixes="ns1">

<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="soap:Envelope/soap:Body/ns1:ProductionToSupply/ns1:ProductionDataList/ns1:ProductionData/ns1:Doc_Number">
<xsl:if test="not(soap:Envelope/soap:Body/ns1:ProductionToSupply/ns1:ProductionDataList/ns1:ProductionData/ns1:Steps)">
    <Steps xmlns="http://somenamespace.com">NA</Steps>
</xsl:if>
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>


</xsl:stylesheet>

期望的输出

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
   <ProductionToSupply xmlns="http://somenamespace.com">
      <ProductionDataList>
         <ProductionData>
            <Material_Number>5</Material_Number>
            <Doc_Number>1234</Doc_Number>
            <Description>abcde</Description>
            <Steps>NA</Steps>
         </ProductionData>
      </ProductionDataList>
   </ProductionToSupply>
</soap:Body>
</soap:Envelope>

预期结果将是仅在“ ProductionData”组中不存在标签“ Steps”时添加标签,但我不知道如何设置适当的匹配模式。

预先感谢您的帮助

如果您只想在不具有Steps元素的情况下匹配组ProductionData ,则模板匹配为:

 <xsl:template match="ns1:ProductionData[not(ns1:Steps)]">

然后,您可以复制ProductionData元素及其子节点,并同时放入Steps元素中

试试这个XSLT

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  
xmlns:ns1="http://somenamespace.com" exclude-result-prefixes="ns1">

<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="ns1:ProductionData[not(ns1:Steps)]">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    <Steps xmlns="http://somenamespace.com">NA</Steps>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

这会将Steps添加为最后一个子级。 如果您想在Doc_Number之后立即使用它,则将模板替换为此

<xsl:template match="ns1:ProductionData[not(ns1:Steps)]/ns1:Doc_Number">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
  <Steps xmlns="http://somenamespace.com">NA</Steps>
</xsl:template>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM