XSLT输出具有不同命名空间的子元素

时间:2016-04-12 03:36:10

标签: xml xslt

我在发布之前在谷歌搜索过但我找不到答案所以,我想在这里发布解决方案,以便对其他开发者有所帮助。

情境: 样本输入

    <CarYard>
       <Cars>
          <A>honda</A>
          <B>BMW</B>
          <C>AUDI</C>
      </Cars>
   </CarYard>$

如何为元素A,B,C生成命名空间 当您进行动态模板匹配时。

2 个答案:

答案 0 :(得分:0)

           <xsl:template match="CarYard">       
            <xsl:apply-templates select="//CarYard/*[1]" mode="addNamespace"/>
        </xsl:element>          
</xsl:template>

<xsl:template match="node() | @*" mode="addNamespaceToAllChild">
    <xsl:variable name="ELEM_NAME" select="concat('ns1:',local-name())"/>
    <xsl:element name="{$ELEM_NAME}" xmlns:ns1="http://test.namespace/generic">
        <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
</xsl:template> 
<xsl:template match="node() | @*" mode="addNamespace">
    <xsl:variable name="ELEM_NAME" select="concat('ns1:',local-name())"/>
    <xsl:element name="{$ELEM_NAME}" xmlns:ns1="http://schemas.test.namespace/generic">
        <xsl:apply-templates select="@* | node()" mode="addNamespaceToAllChild"/>
    </xsl:element>
</xsl:template>

现在结果将是

<CarYard>
   <ns1:Cars>
      <ns1:A>honda</ns1:A>
      <ns1:B>BMW</ns1:B>
      <ns1:C>AUDI</ns1:C>
  </ns1:Cars>

答案 1 :(得分:0)

输出命名空间元素的有效示例就是这样。

    <xsl:element name="x:include" namespace="http://www.w3.org/2001/XInclude">
        <xsl:apply-templates select="@href"/>
    </xsl:element>

哪会产生类似的东西。

<x:include xmlns:x="http://www.w3.org/2001/XInclude" href="../topics/topic.dita">

相关问题