简体   繁体   English

如何使用 XSLT 读取 XML

[英]How to read a XML with XSLT

I need read a CDATA and convert to array.我需要读取 CDATA 并转换为数组。

I have this XML and XSLT and I must validate the content of tag "name" showing the message "MosILOS."我有这个 XML 和 XSLT,我必须验证显示消息“MosILOS”的标签“name”的内容。 when message contains "20200713" in otherwise "otro mensaje".当消息在“otro mensaje”中包含“20200713”时。

I need to program XSLT version 1.0.我需要编写 XSLT 1.0 版。

In the end I drop the expected result最后我放弃了预期的结果

I hope you can help me.我希望你能帮助我。

Thanks in advance提前致谢

XML XML

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header xmlns:v1="http://192.127.0.1/U5g"/>
  <soap:Body xmlns:v1="http://192.127.0.1/U5g/v1">
    <ns2:QueryResponseMessage xmlns:ns2="http://192.127.0.1/5gNext/v1">
      <additionalFields>
        <additionalField>
          <byteValue>
            <![CDATA[<offerList>
  <offer>
    <balanceType>Adicional</balanceType>
    <balance>
      <offerId/>
      <typeCode>Palmera.net</typeCode>
      <total/>
      <endDateTime>2020-02-21T11:55:05</endDateTime>
      <name>TIKTOK POR DIA</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>Palmera.net</typeCode>
      <total/>
      <endDateTime>2020-03-02T11:55:05</endDateTime>
      <name>TIKTOK POR DIA + das a 20200713</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>CEFUR</typeCode>
      <total/>
      <endDateTime>2020-02-22T11:55:05</endDateTime>
      <name>TIKTOK POR DIA</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>C_VPN_STWR</typeCode>
      <total/>
      <endDateTime>2020-02-23T11:55:05</endDateTime>
      <name>TIKTOK POR DIA</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>C_VPN_STWR</typeCode>
      <total/>
      <endDateTime>2020-02-23T11:55:05</endDateTime>
      <name>TIKTOK POR DIA</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>MCV</typeCode>
      <total/>
      <endDateTime>2020-02-24T11:55:05</endDateTime>
      <name>TIKTOK POR DIA</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>MST</typeCode>
      <total/>
      <endDateTime>2020-02-25T11:55:05</endDateTime>
      <name>TIKTOK POR DIA</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>MYT</typeCode>
      <total/>
      <endDateTime>2020-03-01T11:55:05</endDateTime>
      <name>TIKTOK POR DIA</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
  </offer>
  <offer>
    <balanceType>Promocional</balanceType>
  </offer>
  <offer>
    <balanceType>Incluido</balanceType>
  </offer>
  <offer>
    <balanceType>Flex</balanceType>
  </offer>
</offerList>]]>
          </byteValue>
        </additionalField>
      </additionalFields>
      <stackMessages>
        <stackMessage>
          <id>0</id>
          <systemMessage>Successfully</systemMessage>
          <type/>
          <userMessage/>
        </stackMessage>
      </stackMessages>
    </ns2:QueryResponseMessage>
  </soap:Body>
</soap:Envelope>

XSLT XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="exel" version="1.0" xmlns:exel="http://common.org" xmlns:ns0="http://egnext/v6/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" method="xml" version="1.0">
    </xsl:output>
    <xsl:template match="/">
        <xsl:variable name="hala" select="//byteValue/text()"></xsl:variable>
        <xsl:variable name="balance" select="substring-before(substring-after($hala,'&lt;balance&gt;'),'&lt;/balance&gt;')"/>
        <xsl:variable name="typeCode" select="substring-before(substring-after($balance,'&lt;typeCode&gt;'),'&lt;/typeCode&gt;')"/>
        <xsl:variable name="name" select="substring-before(substring-after($balance,'&lt;name&gt;'),'&lt;/name&gt;')"/>

        <umsprot version="1">
            <exec_rsp diagnostic="Successful consulta request" result="OK">
                <data name="id">
                    <xsl:value-of select="//id">
                    </xsl:value-of>
                </data>
                <xsl:choose>
                    <xsl:when test="//id = 0">
                      <xsl:text>Tu paquete incluye: </xsl:text>
                      
                      <xsl:if test="$typeCode = 'Palmera.net'">

                        <xsl:choose>
                          <xsl:when test="contains($name,'20200713')">       
                              <xsl:text>Minutos.</xsl:text>
                          </xsl:when>
                          <xsl:otherwise>
                              <xsl:text>otro mensaje</xsl:text>
                          </xsl:otherwise>  
                        </xsl:choose>

                      </xsl:if>

                    </xsl:when>
                </xsl:choose>
            </exec_rsp>
        </umsprot>
    </xsl:template>
</xsl:stylesheet>

Result结果

<umsprot xmlns:ns0="http:/5gnext/t9" version="9">
   <exec_rsp diagnostic="Successful consulta request" result="OK">
      <data name="id">0</data>incluye:
otro mensaje</exec_rsp>
</umsprot>

The result that I want to get我想得到的结果

<umsprot xmlns:ns0="http://5gnext/z8" version="9">
   <exec_rsp diagnostic="Successful consulta request" result="OK">
      <data name="id">0</data>
      <data name="message">
incluye_
otro mensaje. DROP POR DIA. Expira el 2020/02/21
minutos. DROP POR DIA + 20200713. Expira el 2020/03/02
DROP POR DIA. Expira el 2020/02/22
DROP POR DIA. Expira el 2020/03/01
      </data>

</exec_rsp>
</umsprot>

To do the same thing in one pass, you would need to use a recursive named template in order to process all instances of balance within the CDATA section:要一次性完成相同的事情,您需要使用递归命名模板来处理 CDATA 部分中的所有balance实例:

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns2="http://claro.com.ec/osb/message/Ussd/UssdMessage/v1"
exclude-result-prefixes="soap ns2">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/soap:Envelope">
    <umsprot xmlns:ns0="http://axis/EISApiOnlineWS.wsdl/types/" version="1">
        <exec_rsp diagnostic="Successful consultaBalanceSVA request" result="OK">
            <data name="id">
                <xsl:value-of select="soap:Body/ns2:QueryResponseMessage/stackMessages/stackMessage/id"/>
            </data>
            <data name="message">
                <xsl:text>&#10;Tu paquete incluye: &#10;</xsl:text>
                <xsl:call-template name="process-offerList">
                    <xsl:with-param name="cdata" select="soap:Body/ns2:QueryResponseMessage/additionalFields/additionalField/byteValue"/>
                </xsl:call-template>
            </data>
        </exec_rsp>
    </umsprot>
</xsl:template>

<xsl:template name="process-offerList">
    <xsl:param name="cdata"/>
    <xsl:variable name="balance" select="substring-before(substring-after($cdata, '&lt;balance&gt;'),'&lt;/balance&gt;')"/>
    <xsl:variable name="typeCode" select="substring-before(substring-after($balance,'&lt;typeCode&gt;'),'&lt;/typeCode&gt;')"/>
    <xsl:variable name="name" select="substring-before(substring-after($balance,'&lt;name&gt;'),'&lt;/name&gt;')"/>
    <xsl:variable name="endDateTime" select="substring-before(substring-after($balance,'&lt;endDateTime&gt;'),'&lt;/endDateTime&gt;')"/>
    <xsl:variable name="tail" select="substring-after($cdata, '&lt;/balance&gt;')"/>
    <xsl:if test="$typeCode = 'C_Paquete_Voz_Onnet'">
        <xsl:choose>
            <xsl:when test="contains($name, '5claro')">minutos ILIMITADOS. </xsl:when>
            <xsl:otherwise>otro mensaje. </xsl:otherwise>  
        </xsl:choose>
    </xsl:if>
    <xsl:value-of select="$name"/>
    <xsl:text>. Expira el </xsl:text>
    <xsl:value-of select="translate(substring($endDateTime, 1, 10), '-', '/')"/>
    <xsl:text>&#10;</xsl:text>
    <!-- recursive call -->
    <xsl:if test="contains($tail, '&lt;balance&gt;')">
        <xsl:call-template name="process-offerList">
            <xsl:with-param name="cdata" select="$tail"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

I would suggest you do this in two passes.我建议你分两次完成。

First, apply the following stylesheet to the input XML and save the result to a file:首先,将以下样式表应用于输入 XML 并将结果保存到文件中:

XSLT 1.0 [1] XSLT 1.0 [1]

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns2="http://claro.com.ec/osb/message/Ussd/UssdMessage/v1"
exclude-result-prefixes="soap ns2">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/soap:Envelope">
    <root>
        <id>
            <xsl:value-of select="soap:Body/ns2:QueryResponseMessage/stackMessages/stackMessage/id"/>
        </id>
        <xsl:value-of select="soap:Body/ns2:QueryResponseMessage/additionalFields/additionalField/byteValue" disable-output-escaping="yes"/>    
    </root> 
</xsl:template>

</xsl:stylesheet>

In your example, this would produce:在您的示例中,这将产生:

Interim Result中期结果

<?xml version="1.0" encoding="UTF-8"?>
<root><id>0</id><offerList>
  <offer>
    <balanceType>Adicional</balanceType>
    <balance>
      <offerId/>
      <typeCode>C_Paquete_Voz_Onnet</typeCode>
      <total/>
      <endDateTime>2020-02-21T11:55:05</endDateTime>
      <name>FACEBOOK POR 1 DIA incluye Whatsapp GRATIS</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>C_Paquete_Voz_Onnet</typeCode>
      <total/>
      <endDateTime>2020-03-02T11:55:05</endDateTime>
      <name>FACEBOOK POR 1 DIA incluye Whatsapp GRATIS + llamadas ilimitadas a 5claro movil</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>FBMSN</typeCode>
      <total/>
      <endDateTime>2020-02-22T11:55:05</endDateTime>
      <name>FACEBOOK POR 1 DIA incluye Whatsapp GRATIS</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>C_GPRS_SOCIAL_TWITTER</typeCode>
      <total/>
      <endDateTime>2020-02-23T11:55:05</endDateTime>
      <name>FACEBOOK POR 1 DIA incluye Whatsapp GRATIS</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>C_GPRS_SOCIAL_FACEBOOK</typeCode>
      <total/>
      <endDateTime>2020-02-23T11:55:05</endDateTime>
      <name>FACEBOOK POR 1 DIA incluye Whatsapp GRATIS</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>MCV</typeCode>
      <total/>
      <endDateTime>2020-02-24T11:55:05</endDateTime>
      <name>FACEBOOK POR 1 DIA incluye Whatsapp GRATIS</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>MINST</typeCode>
      <total/>
      <endDateTime>2020-02-25T11:55:05</endDateTime>
      <name>FACEBOOK POR 1 DIA incluye Whatsapp GRATIS</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
    <balance>
      <offerId/>
      <typeCode>MYT</typeCode>
      <total/>
      <endDateTime>2020-03-01T11:55:05</endDateTime>
      <name>FACEBOOK POR 1 DIA incluye Whatsapp GRATIS</name>
      <shortName/>
      <useType>ilimitado</useType>
    </balance>
  </offer>
  <offer>
    <balanceType>Promocional</balanceType>
  </offer>
  <offer>
    <balanceType>Incluido</balanceType>
  </offer>
  <offer>
    <balanceType>Flex</balanceType>
  </offer>
</offerList></root>

Now you can apply a 2nd stylesheet to the resulting file and process it using standard XPath/XSLT methods.现在您可以将第二个样式表应用到生成的文件并使用标准 XPath/XSLT 方法处理它。 This could look something like:这可能看起来像:

XSLT 1.0 [2] XSLT 1.0 [2]

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/root">
    <umsprot xmlns:ns0="http://axis/EISApiOnlineWS.wsdl/types/" version="1">
        <exec_rsp diagnostic="Successful consultaBalanceSVA request" result="OK">
            <data name="id">
                <xsl:value-of select="id"/>
            </data>
            <data name="message">
                <xsl:text>&#10;Tu paquete incluye: &#10;</xsl:text>
                <xsl:for-each select="offerList/offer/balance">
                    <xsl:if test="typeCode = 'C_Paquete_Voz_Onnet'">
                        <xsl:choose>
                            <xsl:when test="contains(name, '5claro')">minutos ILIMITADOS. </xsl:when>
                            <xsl:otherwise>otro mensaje. </xsl:otherwise>  
                        </xsl:choose>
                    </xsl:if>
                    <xsl:value-of select="name"/>
                    <xsl:text>. Expira el </xsl:text>
                    <xsl:value-of select="translate(substring(endDateTime, 1, 10), '-', '/')"/>
                    <xsl:text>&#10;</xsl:text>
                </xsl:for-each>
            </data>
        </exec_rsp>
    </umsprot>
</xsl:template>

</xsl:stylesheet>

to produce:生产:

Final Result最后结果

<?xml version="1.0" encoding="UTF-8"?>
<umsprot xmlns:ns0="http://axis/EISApiOnlineWS.wsdl/types/" version="1">
  <exec_rsp diagnostic="Successful consultaBalanceSVA request" result="OK">
    <data name="id">0</data>
    <data name="message">
Tu paquete incluye: 
otro mensaje. FACEBOOK POR 1 DIA incluye Whatsapp GRATIS. Expira el 2020/02/21
minutos ILIMITADOS. FACEBOOK POR 1 DIA incluye Whatsapp GRATIS + llamadas ilimitadas a 5claro movil. Expira el 2020/03/02
FACEBOOK POR 1 DIA incluye Whatsapp GRATIS. Expira el 2020/02/22
FACEBOOK POR 1 DIA incluye Whatsapp GRATIS. Expira el 2020/02/23
FACEBOOK POR 1 DIA incluye Whatsapp GRATIS. Expira el 2020/02/23
FACEBOOK POR 1 DIA incluye Whatsapp GRATIS. Expira el 2020/02/24
FACEBOOK POR 1 DIA incluye Whatsapp GRATIS. Expira el 2020/02/25
FACEBOOK POR 1 DIA incluye Whatsapp GRATIS. Expira el 2020/03/01
</data>
  </exec_rsp>
</umsprot>

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

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