【问题标题】:How to get the value of an XML tag using XSLT如何使用 XSLT 获取 XML 标记的值
【发布时间】:2020-03-31 16:20:40
【问题描述】:

下午好,

我有以下 XML 作为输入:

<?xml version="1.0" encoding="UTF-8"?>

-<S:Envelope xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">


-<S:Header>


-<wsse:Security S:mustUnderstand="1">


-<wsu:Timestamp wsu:Id="_1" xmlns:ns13="http://www.w3.org/2003/05/soap-envelope" xmlns:ns14="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" xmlns:ns15="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">

<wsu:Created>2020-03-27T19:04:20Z</wsu:Created>

<wsu:Expires>2020-03-27T19:09:20Z</wsu:Expires>

</wsu:Timestamp>

</wsse:Security>

</S:Header>


-<S:Body>


-<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">

<faultcode>S:Server</faultcode>

<faultstring>Ocorreu um erro no serviço</faultstring>


-<detail>


-<ns3:DataLicensingFault xmlns:ns3="http://services.experian.com.br/DataLicensing/DataLicensingService/" xmlns:ns2="http://www.experian.com.br/schema/infocleanws">

<codeReason>100</codeReason>

<reason>Parâmetro cnpj é inválido.</reason>

</ns3:DataLicensingFault>

</detail>

</S:Fault>

</S:Body>

</S:Envelope>

我需要获取在 codeReason 标记中找到的值。

为此,我使用以下命令

<xsl: variable name = "vCodeReason" select = "./ soapenv: Fault / detail / DataLicensingFault / codeReason" />

但我得到了 vCodeReason 变量的白色值。

正确的做法是让我得到值 100。

我正在发送 xslt 程序:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output omit-xml-declaration="no" indent="yes" encoding="utf-8"/>
   <xsl:template match="/">
      <!-- Campos do Datatype da Interface :-->
      <xsl:variable name="vFaultCode" select="./soapenv:Fault/faultcode"/>
      <xsl:variable name="vFaultString" select="./soapenv:Fault/faultstring"/>
      <xsl:variable name="vCodeReason" select="./soapenv:Fault/detail/DataLicensingFault/codeReason"/>
      <xsl:variable name="vReason" select="./soapenv:Fault/detail/DataLicensingFault/reason"/>
      <xsl:choose>
         <xsl:when test="$vFaultCode!=&apos;&apos;">
            <ns0:ConsultarPJResponse xmlns:ns0="http://services.experian.com.br/DataLicensing/DataLicensingService/">
              <result>
                 <situacaoCadastral>
                     <codigoSituacao>
                        <xsl:value-of select="$vCodeReason"/>
                     </codigoSituacao>
                     <situacao>
                        <xsl:value-of select="$vFaultString"/>
                     </situacao>
                 </situacaoCadastral>
              </result>
            </ns0:ConsultarPJResponse>
         </xsl:when>
         <xsl:otherwise>
            <xsl:copy-of select="/"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

你能帮帮我吗?

【问题讨论】:

  • 请发布minimal reproducible example,以便我们在定义变量时查看当前上下文。注意DataLicensingFault在命名空间中,所以在引用它时必须使用前缀。
  • 对不起。我正在发送 xslt 程序

标签: xml xslt tags


【解决方案1】:

您的模板与 / 根节点匹配,而 soapenv:Fault 不是该节点的子节点。这是我在关于DataLicensingFault 命名空间的评论中所说的补充内容。

如果你愿意,你可以这样做:

<xsl:variable name="vCodeReason" select="//codeReason"/>

要使用当前节点的显式路径,您需要这样做:

<xsl:variable name="vCodeReason" select="S:Envelope/S:Body/S:Fault/detail/ns3:DataLicensingFault/codeReason"/>

并在stylesheet 开始标记中包含两个命名空间声明。


演示https://xsltfiddle.liberty-development.net/3MvmXiA

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2012-01-21
    • 2015-11-09
    • 2018-03-20
    • 1970-01-01
    相关资源
    最近更新 更多