3

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

问题是我有一个声明了命名空间的输入 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>

生成的 xml 应如下所示:

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

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

我的 XSLT 看起来像这样:

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

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

PS:谁编辑了我的条目:谢谢:)

4

4 回答 4

2

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

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

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

这是一个优秀的 ZVON 教程的链接,它有工作示例: http ://www.zvon.org/xxl/XSLTreference/Output/xslt_element_namespace.html

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

编辑我想你会在这里找到你的答案: XSLT root tag namespace instead of element attribute namespace

于 2009-10-22T07:51:25.470 回答
1
<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>
于 2009-10-22T09:28:25.587 回答
0

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

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

另外,阅读这篇关于 XSLT 中的命名空间的文章

于 2009-10-22T17:12:29.540 回答
0
<xsl:template match="a:*">
  <xsl:element name="{local-name()}"
               namespace="http://example.com/B">
    <xsl:copy-of select="@*" />
    <xsl:apply-templates />
  </xsl:element>
</xsl:template>

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

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

于 2009-10-22T07:51:37.230 回答