首页 > 解决方案 > 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

解决方案


是的,我在 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>    

推荐阅读