我需要读取CDATA并转换为数组。
我拥有此XML和XSLT,我必须验证标记“ name”的内容,该消息显示消息“ Minutos ILIMITADOS”。当消息包含“ 5claro”时,否则为“ otro mensaje”。
我需要编写XSLT版本1.0。
最后我放弃了预期的结果
希望您能帮助我。
提前感谢
XML
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header xmlns:v1="http://claro.com.ec/osb/message/Ussd/UssdMessage/v1"/>
<soap:Body xmlns:v1="http://claro.com.ec/osb/message/Ussd/UssdMessage/v1">
<ns2:QueryResponseMessage xmlns:ns2="http://claro.com.ec/osb/message/Ussd/UssdMessage/v1">
<additionalFields>
<additionalField>
<byteValue>
<![CDATA[<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>]]>
</byteValue>
</additionalField>
</additionalFields>
<header>
<externalTransactionDate>2020-01-30T19:52:42</externalTransactionDate>
<externalTransactionId>121323</externalTransactionId>
<internalTransactionId>b2b5a8cb-6a2d-4a11-b75a-395036e5b2aa</internalTransactionId>
<responseTime/>
<token>0097f464-acb5-43ec-008b-14aebeae2296</token>
</header>
<stackMessages>
<stackMessage>
<id>0</id>
<systemMessage>Operation Executed Successfully</systemMessage>
<type/>
<userMessage/>
</stackMessage>
</stackMessages>
</ns2:QueryResponseMessage>
</soap:Body>
</soap:Envelope>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="exsl" version="1.0" xmlns:exsl="http://exslt.org/common" xmlns:ns0="http://axis/EISApiOnlineWS.wsdl/types/" 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,'<balance>'),'</balance>')"/>
<xsl:variable name="typeCode" select="substring-before(substring-after($balance,'<typeCode>'),'</typeCode>')"/>
<xsl:variable name="name" select="substring-before(substring-after($balance,'<name>'),'</name>')"/>
<umsprot version="1">
<exec_rsp diagnostic="Successful consultaBalanceSVA 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 = 'C_Paquete_Voz_Onnet'">
<xsl:choose>
<xsl:when test="contains($name,'5claro')">
<xsl:text>Minutos ILIMITADOS.</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>
结果
<umsprot xmlns:ns0="http://axis/EISApiOnlineWS.wsdl/types/" version="1">
<exec_rsp diagnostic="Successful consultaBalanceSVA request" result="OK">
<data name="id">0</data>Tu paquete incluye:
otro mensaje</exec_rsp>
</umsprot>
我想要得到的结果
<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>
要一次性完成相同的操作,您需要使用递归的命名模板,以便处理CDATA部分中的balance
的所有实例:
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> Tu paquete incluye: </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, '<balance>'),'</balance>')"/>
<xsl:variable name="typeCode" select="substring-before(substring-after($balance,'<typeCode>'),'</typeCode>')"/>
<xsl:variable name="name" select="substring-before(substring-after($balance,'<name>'),'</name>')"/>
<xsl:variable name="endDateTime" select="substring-before(substring-after($balance,'<endDateTime>'),'</endDateTime>')"/>
<xsl:variable name="tail" select="substring-after($cdata, '</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> </xsl:text>
<!-- recursive call -->
<xsl:if test="contains($tail, '<balance>')">
<xsl:call-template name="process-offerList">
<xsl:with-param name="cdata" select="$tail"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
我建议您分两次通过。
首先,将以下样式表应用于输入XML,并将结果保存到文件中:
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>
在您的示例中,这将产生:
中期结果
<?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>
现在您可以将第二个样式表应用于生成的文件,并使用标准XPath / XSLT方法对其进行处理。可能看起来像:
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> Tu paquete incluye: </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> </xsl:text>
</xsl:for-each>
</data>
</exec_rsp>
</umsprot>
</xsl:template>
</xsl:stylesheet>
产生:
最终结果
<?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>