xslt - XSLT 转换验证 XSI:类型(或)元素节点是否存在
问题描述
'ns1:getGenResponse'
我有一个输入 XML,如果元素节点存在(或)使用xsi:type = "Gen"
“multiRef”元素进行验证,则需要对其进行过滤
如果其中任何一个条件成功,那么我可以处理'multiRef'
记录。
输入 XML:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<soapenv:Body>
<ns1:getGenResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://service.pen.eewe.en.de>
<ns1:getGenReturn xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:Array" soapenc:arrayType="xsd:anyType[2]">
<item href="#id0" />
<item href="#id1" />
</ns1:getGenReturn>
</ns1:getGenResponse>
<multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Gen">
<name xsi:type="xsd:string">ulm</name>
<mail xsi:type="xsd:string">ulm@gmail.com</mail>
</multiRef>
<multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id1" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:Gen">
<name xsi:type="xsd:string">ABC</name>
<mail xsi:type="xsd:string">abc@gmail.com</mail>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>
尝试使用节点存在'ns1:getGenResponse':
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:response="http://tempuri.org/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="soap response"
>
<!-- Output -->
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<getGenResponse>
<xsl:for-each select="//soap:Body[ns1:getGenResponse]/multiRef">
<getGenReturn>
<name>
<xsl:value-of select="name" />
</name>
<mail>
<xsl:value-of select="mail" />
</mail>
</getGenReturn>
</xsl:for-each>
</getGenResponse>
</xsl:template>
</xsl:stylesheet>
使用这个 XSLT,我可以生成空白 . 我无法产生我想要的输出。
我还尝试使用以下代码使用 xsi:type of 'multiRef' 元素节点验证数据,但我无法执行 XSLT
**Trying to validate with xsi:type ="Gen"**
<xsl:stylesheet version="1.0" xmlns:response="http://tempuri.org/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="soap response">
<!-- Output -->
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<getGenResponse>
<xsl:for-each select="//soap:Body/multiRef[substring- after(@xsi:type, ':')='Gen']">
<getGenReturn>
<name><xsl:value-of select="name"/></name>
<mail><xsl:value-of select="mail"/></mail>
</getGenReturn>
</xsl:for-each>
</getGenResponse>
</xsl:template>
</xsl:stylesheet>
输出预期:
**Output Expected**
<?xml version="1.0" encoding="UTF-8"?>
<getGenResponse>
<getGenReturn>
<name> ULM </name>
<mail>ulm@gmail.com<mail>
</getGenReturn>
<getGenReturn>
<name>ABC</name>
<mail>abc@gmail.com<mail>
</getGenReturn>
/getGenResponse>
非常感谢。
解决方案
是的,我在 XSLT 中声明了 xmlns:ns1="http://service.pen.eewe.en.de"。现在我能够以所需的结果执行代码。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:response="http://tempuri.org/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="soap response" xmlns:ns1="http://service.pen.eewe.en.de"
>
<!-- Output -->
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<getGenResponse>
<xsl:for-each select="//soap:Body[ns1:getGenResponse]/multiRef">
<getGenReturn>
<name>
<xsl:value-of select="name" />
</name>
<mail>
<xsl:value-of select="mail" />
</mail>
</getGenReturn>
</xsl:for-each>
</getGenResponse>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- tensorflow - 如何在使用 Residual Skip Connections(基于 ResNet 的自动编码器)的 Keras(tensorflow.keras)中构建自动编码器/U-Net?
- javascript - 如何在 javascript 或 jquery 中提交一组值?
- javascript - 将带有图像的 html 表导出到 xlsx - Javascript
- node.js - 如何在 Google Cloud Function 中通过 HTTP 响应发送大文件
- swift - Swift - 如何使用枚举字典初始化变量?
- r - 如何更改颜色索引中的中间值?
- selenium - 如何使用 java 处理 Selenium 中的自动建议?
- udp - 有没有办法通过 udp 检查来自源的 rtp 流?
- mysql - “一般错误:1364 字段‘user_id’没有默认值”Laravel
- azure-sql-database - 数据迁移 - 通过 Azure 数据工厂从 Cosmos DB 到 Azure SQL 数据库