简体   繁体   English

如何使用 XSLT 获取 XML 标记的值

[英]How to get the value of an XML tag using XSLT

Good afternoon,下午好,

I have the following XML as input:我有以下 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>

I need to get the value found in the codeReason tag.我需要获取在 codeReason 标签中找到的值。

For this I am using the following command为此,我使用以下命令

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

But I'm getting the white value for the vCodeReason variable.但是我得到了 vCodeReason 变量的白色值。

The correct thing would be for me to get the value 100.正确的做法是让我获得 100 的值。

I am sending the xslt program:我正在发送 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>

Can you help me?你能帮助我吗?

Your template matches the / root node, and soapenv:Fault is not a child of that node.您的模板与/根节点匹配,并且soapenv:Fault不是该节点的子节点。 This is in addition to what I said in the comment regarding the namespace of DataLicensingFault .这是我在关于DataLicensingFault命名空间的评论中所说的DataLicensingFault

If you like, you could do simply:如果你愿意,你可以简单地做:

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

To use an explicit path from the current node, you need to do:要使用当前节点的显式路径,您需要执行以下操作:

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

and include the two namespace declarations in the stylesheet start-tag.并在stylesheet开始标记中包含两个命名空间声明。


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

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

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