2016-09-23 2 views
0

У меня есть входной XML-адрес, где S: Fault xmlns: ns4 = "http://www.w3.org/2003/05/soap-envelope", что создает проблему для извлечения данных из узла сбоя. .Проблема с пространством имен

<?xml version="1.0" encoding="UTF-8"?> 
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope"> 
      <faultcode>S:Server</faultcode> 
      <faultstring>The GTIN is not valid or the system can not map the Company Prefix to an existing Company Prefix from the Setting</faultstring> 
     </S:Fault> 
    </S:Body> 
</S:Envelope> 

XLST код не работает ...

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="example" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns4="http://www.w3.org/2003/05/soap-envelope" 
exclude-result-prefixes="S ns4"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

    <xsl:template match="/S:Envelope"> 
      <ns0:MT_CreateSerialNumberResponse_IB xmlns:ns0="example"> 
      <serialNumberList xmlns="urn:abcd:1"> 
       <body> 
        <message> 
         <xsl:value-of select="S:Body/ns4:Fault/ns4:faultstring"/> 
        </message> 
       </body> 
      </serialNumberList> 
     </ns0:MT_CreateSerialNumberResponse_IB> 
    </xsl:template> 
</xsl:stylesheet> 

Ожидаемый результат ... Пожалуйста, помогите найти ошибку кода, если я что-то пропустил

<ns0:MT_CreateSerialNumberResponse_IB xmlns:ns0="example"> 
    <serialNumberList xmlns="urn:abcd:1"> 
     <body><message>The GTIN is not valid or the system can not map the Company Prefix to an existing Company Prefix from the Setting</message> 
     </body> 
    </serialNumberList> 
</ns0:MT_CreateSerialNumberResponse_IB> 

ответ

1

Попробуйте

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="example" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns4="http://www.w3.org/2003/05/soap-envelope" 
exclude-result-prefixes="S ns4"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

    <xsl:template match="/S:Envelope"> 
      <ns0:MT_CreateSerialNumberResponse_IB xmlns:ns0="example"> 
      <serialNumberList xmlns="urn:abcd:1"> 
       <body> 
        <message> 
         <xsl:value-of select="S:Body/S:Fault/faultstring"/> 
        </message> 
       </body> 
      </serialNumberList> 
     </ns0:MT_CreateSerialNumberResponse_IB> 
    </xsl:template> 
</xsl:stylesheet> 
+0

Существует бесплатный онлайн инструмент для проверки: XSLT HTTP: // xslttest. appspot.com/ – Naidu

1
  1. Там нет узла во входном XML, имя которого префикс ns4:. Это делает объявление пространства имен xmlns:ns4="http://www.w3.org/2003/05/soap-envelope" полностью избыточным (как в XML, так и в XSLT).

  2. Только по умолчанию декларации пространства имен (без префикса) наследуются. Элемент faultstring не имеет префикса и не объявляет об объявлении пространства имен по умолчанию. Это означает, что в не-пространстве имен, и путь к нему (из контекста S:Envelope) является:

    S:Body/S:Fault/faultstring

Смежные вопросы