XSLT 避免命名空间 [英] XSLT to avoid namespace

查看:26
本文介绍了XSLT 避免命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像 .. 的输入 xml:

I have a input xml like..:

 <?xml version="1.0" encoding="UTF-8"?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ListResponse xmlns="urn:abcde:xyz:1">
                <Gtins>
                    <Gtin>
                        <gtinID>11111</gtinID>
                        <name>222222</name>
                        <label>S11111 - EA</label>
                        <description>XYZ</description>
                        <value>11111</value>
                    </Gtin>
                    <Gtin>
                        <gtinID>999999</gtinID>
                        <name>999999</name>
                        <label>asdfg</label>
                        <description>ghgj</description>
                        <value>999999</value>
                    </Gtin>
                </Gtins>
            </ListResponse>
        </S:Body>
    </S:Envelope>

如何通过 XSLT 选择节点 'Gtin' 的每个值并避免命名空间?

How do I select each and every value of the node 'Gtin' and avoid namespace through XSLT?

输出 XML 应该是...

Output XML should be...

<ns0:RFC xmlns:ns0="http://asd.com">
    <Gtins>
      <Gtin>
        <gtinID>11111</gtinID>
        <name>222222</name>
        <label>S11111 - EA</label>
        <description>XYZ</description>
        <value>11111</value>
      </Gtin>
      <Gtin>
        <gtinID>999999</gtinID>
        <name>999999</name>
        <label>asdfg</label>
        <description>ghgj</description>
        <value>999999</value>
      </Gtin>
     </Gtins>   
  </ns0:RFC>

推荐答案

结论/总结评论和其他已经回答的问题.

Conclusion / Summary of comments and other already answered questions.

在@michael.hor257k 的建议下,请访问此线程.接受与您相同问题的答案.命名空间的使用

In advice of @michael.hor257k please visit this thread. Accepted answer to the same problem as you have. Use of namespace

问题:您的 XML 将其元素放在命名空间中.

The problem: your XML puts its elements in a namespace.

解决方案:在样式表中声明相同的命名空间,为其分配一个前缀并使用该前缀来寻址源 XML 中的元素.

Solution: declare the same namespace in your stylesheet, assign it a prefix and use that prefix to address the elements in the source XML.


根据您给定的输入,开始并继续使用此基础:


Due to your given input, start and continue on your own with this base:

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:abc="urn:abcde:xyz:1">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/S:Envelope">
        <!-- select any child - exemplary -->
        <xsl:value-of select="S:Body/abc:ListResponse/abc:Gtins/abc:Gtin/abc:gtinID" />
    </xsl:template>
    
</xsl:stylesheet>


此外:

这是您的 input-xml 的等效版本:

This is an equivalent version of your input-xml:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="urn:abcde:xyz:1">
    <S:Body>
        <abc:ListResponse>
            <abc:Gtins>
                <abc:Gtin>
                    <abc:gtinID>11111</abc:gtinID>
                    <abc:name>222222</abc:name>
                    <abc:label>S11111 - EA</abc:label>
                    <abc:description>XYZ</abc:description>
                    <abc:value>11111</abc:value>
                </abc:Gtin>
                <abc:Gtin>
                    <abc:gtinID>999999</abc:gtinID>
                    <abc:name>999999</abc:name>
                    <abc:label>asdfg</abc:label>
                    <abc:description>ghgj</abc:description>
                    <abc:value>999999</abc:value>
                </abc:Gtin>
            </abc:Gtins>
        </abc:ListResponse>
    </S:Body>
</S:Envelope>


命名空间的存在是有原因的!使用它们,不要仅仅因为我不理解它们,所以我想删除它们"-原因而摆脱它们.不要误会我的意思,你的 result-xml 可以没有命名空间,这一点毫无疑问.有很多关于 SO 的例子,去吧.

这篇关于XSLT 避免命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆