2017-08-29 42 views
0

以下是我的要求 我有订单输入包含项目,每个项目包含子项目,但标签名称是相同的“项目”。如果父母和孩子的xml标签在xslt中具有相同的名称,如何保持从源到目标的相同结构

<OrderInput> 
    <Item> 
     <ItemId> 
     <ItemName> 
     <ItemDesc> 
     <Item> 
      <ItemId> 
      <ItemName> 
      <ItemDesc> 
      <Item> 
       <ItemId> 
       <ItemName> 
       <ItemDesc> 
      </Item> 
     </Item> 
    </Item> 
    <Item> 
     <ItemId> 
     <ItemName> 
     <ItemDesc> 
     <Item> 
      <ItemId> 
      <ItemName> 
      <ItemDesc> 
     </Item> 
    </Item> 
</OrderInput> 

使用xslt进行转换后,它应该如下所示。父和chil节点具有相同的名称“线”,如源“项”

<OrderOutput> 
    <OrderLine> 
    <Line> 
     <LineId> 
     <LineName> 
     <LineDesc> 
     <Line> 
      <LineId> 
      <LineName> 
      <LineDesc> 
      <Line> 
       <LineId> 
       <LineName> 
       <LineDesc> 
      </Line> 
     </Line> 
    </Line> 
    <Line> 
     <LineId> 
     <LineName> 
     <LineDesc> 
     <Line> 
      <LineId> 
      <LineName> 
      <LineDesc> 
     </Line> 
    </Line> 
</OrderOutput> 

请帮我解决这个问题。

+1

在这情况下'OrderLine'输出来自和它为什么关闭? –

+0

对不起..我错过了它..请假设 –

回答

1

您可以使用元素名称替换而转化

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0"> 
    <xsl:output indent="yes"/> 

    <xsl:template match="OrderInput"> 
     <OrderOutput> 
      <xsl:apply-templates/> 
     </OrderOutput> 
    </xsl:template> 

    <xsl:template match="*[starts-with(name(.), 'Item')]"> 
     <xsl:element name="{replace(name(.), '^Item', 'Line')}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 
+0

您好Rupesh ..感谢您的快速回复...我的要求是不是字符串的替换......只是为了说明目的,我给了输出格式。 –

+0

@VamsiKrishna因为你可以简单地将元素转换为元素映射,对于元素嵌套如果你不改变文档的映射比它不会影响输出。就像上面代码的第一个模板一样。 – Rupesh

相关问题