简体   繁体   English

如何从wso2中的xml中检索元素的名称空间

[英]How to retrieve name-space of an element from an xml in wso2

XML: XML:

<Report xmlns="https://defaultnamespace/abc">     
<Conversion>
<Type>FirstElement</Type>
</Conversion>
<Conversion>
<Type>SecondElement</Type>
</Conversion>
<Conversion>
<Type>ThirdElement</Type>
</Conversion>
</Report>

wso2 code: wso2代码:

<inSequence>
<iterate xmlns:cs="https://defaultnamespace/abc" continueparent="true" expression="//cs:Report/cs:Conversion" attachPath="//cs:Report">
<target>
<sequence>
<xslt ket="SampleXSLT.xslt"/>
</sequence>
</target>
</iterate>
</inSequence>

The issue is the above iterate mediator works only if the input XML as above works only if it contains namespace " https://defaultnamespace/abc ". 问题是,仅当上述输入XML包含名称空间“ https:// defaultnamespace / abc ”时,上述迭代中介器才有效。

Requirement: 需求:

The iterate mediator does not work if the above XML has different namespace. 如果上述XML具有不同的名称空间,则迭代中介器将不起作用。 So, I need alternative solutions so that the iterate mediator takes namespace that comes from XML dynamically and the transformation happens. 因此,我需要替代解决方案,以便迭代介体动态获取来自XML的名称空间并进行转换。 Help me with the best solution with minor modification in the code. 在代码中稍加修改即可为我提供最佳解决方案。

New Requirement: 新要求:

I have a similar requirement in XSLT also. 我在XSLT中也有类似的要求。

XML: XML:

<Report xmlns="https://defaultnamespace/abc">     
<Conversion>
<Type>FirstElement</Type>
</Conversion>
<Conversion>
<Type>SecondElement</Type>
</Conversion>
<Conversion>
<Type>ThirdElement</Type>
</Conversion>
<Last>This is last element</Last>
</Report>

XSLT: XSLT:

<?xml version=1.0 encoding=UTF-8?>
<xsl:stylesheet xmlns:cs="https://defaultnamespace/abc">
<xsl:template match="/">
<xsl:for-each select="/cs:Report/cs:Conversion/cs:Type">
<xsl:element name="Converting"><xsl:value-of select="."/></xsl:element>
</xsl:for-each>
<xsl:element name="LastOne"><xsl:value-of select="/cs:Report/cs:Last"/>               </xsl:element>
</xsl:template>
</xsl:stylesheet>

The above XSLT fails when the input XML has a different namespace. 当输入XML具有不同的名称空间时,上述XSLT将失败。 So, I need an XSLT that will dynamically get the namespace from input XML and does the transformation. 因此,我需要一个XSLT,它将从输入XML中动态获取名称空间并进行转换。

 <iterate preservePayload="true"
          attachPath="//*[local-name()='Report']"
          expression="//*[local-name()='Report']/*[local-name()='Conversion']">
    <target>
       <sequence>
          <xslt ket="SampleXSLT.xslt"/>
       </sequence>
    </target>
 </iterate>

if you want to use actual namespace from the message : 如果要使用消息中的实际名称空间:

<property name="ns" expression="namespace-uri($body/*)"/>
<iterate preservePayload="true"
         attachPath="//*[namespace-uri()=$ctx:ns and local-name()='Report']"
         expression="//*[namespace-uri()=$ctx:ns and local-name()='Report']/*[namespace-uri()=$ctx:ns and local-name()='Conversion']">

How to retrieve namespaces in XML files using Xpath 如何使用Xpath检索XML文件中的名称空间

I think the second answer in the above post shall answer your question.. 我认为以上帖子中的第二个答案将回答您的问题。

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

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