在 XSL 转换中更改命名空间值? [英] Changing a namespace value in an XSL transformation?

查看:23
本文介绍了在 XSL 转换中更改命名空间值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可行,因为我对 XSLT 和其他东西很陌生,但也许你们中的一些人可以帮助我?这有点棘手,我还没有在互联网上找到类似的东西:

I'm not sure if that is possible, as I'm very new to XSLT and stuff, but maybe some of you could help me here? It's a bit tricky and I haven't found anything like it on the internet:

问题是我有一个声明了命名空间的输入 xml,我只需要对其进行轻微更改(添加或删除属性,或将它们移动到其他位置).但与此同时,我必须更新文档的文档标签中的命名空间引用.因此,例如,输入 xml 可能如下所示:

The problem is that I have an input xml with namespaces declared and all and I only need to make slight changes to it (adding or deleting attributes, or shifting them to other locations). But at the same time, I have to update the namespace references in the document's document tag. So, for example, the input xml might look something like this:

<order
  xmlns="some.url.01"
  xmlns:ns2="some.other.url"
  xmlns:ns3="another.one"
>
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <timestamp>timestamp</timestamp>
      <requestedDocuments>
        <ns2:document>orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>

生成的 xml 应如下所示:

the resulting xml should look like this:

<order
  xmlns="some.url.02"
  xmlns:ns2="some.other.url.02"
  xmlns:ns3="another.one.02"
>
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <!-- deleted timestamp for example -->
      <requestedDocuments>
        <ns2:document>orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>

但我唯一得到的是:

<order
  xmlns="some.url.02"
>
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <!-- deleted timestamp for example -->
      <requestedDocuments>
        <ns2:document xmlns:ns2="some.other.url.02">orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>

现在也许对你们中的一两个人来说可能没什么大不了的,但我有一个限制,即输出文档应该与输入文档一一对应,除了请求的更改(命名空间更改和删除).

Now maybe for one or two of you it might not be that big a deal, but I have the restriction that the output document should look one-to-one the same as the input document except for the requested changes (namespace changes and deletion).

我的 XSLT 看起来像这样:

My XSLT looks a like this:

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="some.url.02"
  xmlns:ns2="some.other.url.02"
  xmlns:ns3="another.one.02"
>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>

  <xsl:strip-space elements="*"/>

  <xsl:template match="*">
    <xsl:choose>
      <xsl:when test="name(.) != 'timestamp'">
        <xsl:element name="{node-name(.)}">
          <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:attribute name="{node-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

有人可以帮忙吗?命名空间很棘手:(

Can somebody please help? Namespaces are tricky :(

P.S.:谁编辑了我的条目:谢谢:)

P.S.: Whoever edited my entry: Thanks :)

推荐答案

您可以使用命名空间属性在输出元素上设置命名空间:

You can set the namespace on the output element with the namespace attribute:

<xsl:element name="{node-name(.)}" namespace="http://www.bar.org">
  // ...
</xsl:element>

请注意,命名空间必须是一个 URI,虽然我希望您知道这一点,但在您的示例中使用 URI 可能是个好主意.

Note that the namespace must be a URI and although I expect you know this it's probably a good idea to use URIs in your example.

这里是优秀的 ZVON 教程的链接,该教程已提供示例:http://www.zvon.org/xxl/XSLTreference/Output/xslt_element_namespace.html

Here is a link to the excellent ZVON tutorial which has worked examples: http://www.zvon.org/xxl/XSLTreference/Output/xslt_element_namespace.html

我同意命名空间很棘手.如您所知,前缀在语义上无关紧要,但许多系统允许您出于美学原因选择前缀.也看看撒克逊 (http://saxon.sourceforge.net/)

I agree that namespaces are tricky. As you know the prefix is semantically irrelevant, but many systems allow you to choose your prefix for aesthetic reasons. Also look at Saxon (http://saxon.sourceforge.net/)

编辑我想你会在这里找到你的答案:XSLT 根标记命名空间而不是元素属性命名空间

EDIT I think you will find your answer here: XSLT root tag namespace instead of element attribute namespace

这篇关于在 XSL 转换中更改命名空间值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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