Xslt 在XSL转换中更改名称空间值?

Xslt 在XSL转换中更改名称空间值?,xslt,xml-namespaces,Xslt,Xml Namespaces,我不确定这是否可行,因为我对XSLT和其他东西非常陌生,但也许你们中的一些人可以在这里帮助我?这有点棘手,我在互联网上还没有找到类似的东西: 问题是,我有一个声明了名称空间和all的输入xml,我只需要对它做一些轻微的更改(添加或删除属性,或者将它们转移到其他位置)。但同时,我必须更新文档的文档标记中的名称空间引用。例如,输入xml可能如下所示: <order xmlns="some.url.01" xmlns:ns2="some.other.url" xmlns:ns3="a

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

问题是,我有一个声明了名称空间和all的输入xml,我只需要对它做一些轻微的更改(添加或删除属性,或者将它们转移到其他位置)。但同时,我必须更新文档的文档标记中的名称空间引用。例如,输入xml可能如下所示:

<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>
<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>
<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>

测试
测试
测试
时间戳
订单
生成的xml应如下所示:

<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>
<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>
<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>

测试
测试
测试
订单
但我唯一得到的是:

<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>

测试
测试
测试
订单
现在,也许对你们中的一两个人来说,这可能没什么大不了的,但我有一个限制,即输出文档应该与输入文档一对一地看起来相同,除了请求的更改(名称空间更改和删除)

我的XSLT看起来像这样:

<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>
<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>
<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>

有人能帮忙吗?名称空间很棘手:(


注意:编辑我的条目的人:谢谢:)

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

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

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

以下是优秀ZVON教程的链接,该教程提供了一些有用的示例:

我同意名称空间很棘手。正如您所知,前缀在语义上是不相关的,但是许多系统允许您出于美观的原因选择前缀。再看看撒克逊人()

编辑我想你会在这里找到答案:


它在名称空间中搜索前缀为
a
的任何元素,并将其替换为名称空间名称相同的元素
http://example.com/B
。所有属性都按“原样”复制,然后计算所有子级

根据需要在其中或周围添加自定义处理。


<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:ns1_src="some.url.01"
  xmlns:ns2_src="some.other.url"
  xmlns:ns3_src="another.one"
  xmlns="some.url.02"
  xmlns:ns2="some.other.url.02"
  xmlns:ns3="another.one.02"
>
  <!-- 
    Note that all the source namespaces got their own new "*_src" prefix. 
    The target namespaces take over the original prefixes. 
    "some.url.02" is the new global namespace.
  -->

  <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!-- the identity template to copy everything, unless 
       it has been declared otherwise -->
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>

  <!-- three templates to handle elements -->
  <xsl:template match="ns1_src:*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="node() | @*" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="ns2_src:*">
    <xsl:element name="ns2:{local-name()}">
      <xsl:apply-templates select="node() | @*" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="ns3_src:*">
    <xsl:element name="ns3:{local-name()}">
      <xsl:apply-templates select="node() | @*" />
    </xsl:element>
  </xsl:template>

  <!-- three templates to handle attributes -->
  <xsl:template match="@ns1_src:*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="." />
    </xsl:attribute>
  </xsl:template>

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

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

  <!-- timestamps will be ignored -->
  <xsl:template match="ns1_src:timestamp" />

</xsl:stylesheet>
输出:

<order xmlns="some.url.02">
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <requestedDocuments>
        <ns2:document xmlns:ns2="some.other.url.02">orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>

测试
测试
测试
订单

您是否使用Ant的XSLT任务进行转换

如果答案是肯定的,您可能希望从Sun JDK 1.5+附带的默认XSLT引擎切换。阅读


另外,阅读XSLT中的名称空间

您可以将URI存储在
中,并像
名称空间=“{$URI}”
一样使用它。如果你更经常需要它,那就太好了。如果我说的没错,那么这也会导致相同的XML,在标签中使用这些名称空间,对吗?谢谢你无数次!那个链接确实帮助了我,现在它起作用了,耶!=)@Boldewyn-您不需要使用属性值模板{…}作为名称吗?不幸的是,当我将其添加到XSLT中时,它会创建与以前完全相同的xml,在每个元素中使用该名称空间,而不是在根元素中声明一次名称空间(本例中的顺序)=(@peter.murray.rust:噢,是的,你说得对。对不起,Wickermoon,我马上更新我的答案。@Wickermoon:好的,现在它做它所宣传的事情。将命名空间a的元素作为命名空间B的输入和输出同义元素。顺便说一句:这次我在Saxon 9中测试了它。什么。谢谢你的帮助,但我认为我解释得不够清楚,so让我再试一次:您给我的结果输出就是我得到的结果,但我想要的是xmlns:ns2声明在order标记中。=/@Wickermoon-我回答中的新链接可能会帮助您了解这些链接,但不是,我实际上使用java和Saxon9。一些东西来完成转换。:)