2014-04-04 3 views
0

Я пытаюсь изменить имя узла XML, но это не позволяет мне это делать. В моем нижнем коде у меня есть два шаблона: 1. Изменить имя узла 2.Создать родительский узел для DocumentReference. См. Мои XML и XSLT.Изменение имени элемента XML с помощью XSLT

Мой XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    <DataArea> 
    <PurchaseOrder> 
     <PurchaseOrderLine> 
      <DocumentReference> 
       <DocumentID> 
        <ID>23423</ID> 
       </DocumentID> 
      </DocumentReference> 
      <DocumentReference> 
       <DocumentID> 
        <ID>23424</ID> 
       </DocumentID> 
      </DocumentReference> 
      <Item> 
       <CustomerItemID> 
        <!-- ArtNr --> 
        <ID>444</ID> 
       </CustomerItemID> 
      </Item> 
      <Quantity unitCode="PCE">17.3</Quantity> 
     </PurchaseOrderLine> 
    </PurchaseOrder> 
    </DataArea> 

Ожидаемый результат

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    <DataArea> 
    <PurchaseOrder> 
     <POL> 
      <DocumentReference> 
       <DocumentID> 
        <ID>23423</ID> 
       </DocumentID> 
      </DocumentReference> 
      <DocumentReference> 
       <DocumentID> 
        <ID>23424</ID> 
       </DocumentID> 
      </DocumentReference> 
      <Item> 
       <CustomerItemID> 
        <!-- ArtNr --> 
        <ID>444</ID> 
       </CustomerItemID> 
      </Item> 
      <Quantity unitCode="PCE">17.3</Quantity> 
     </POL> 
    </PurchaseOrder> 
    </DataArea> 

Мой XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="PurchaseOrderLine"> 
     <POL> 
     <xsl:apply-templates /> 
     </POL> 
    </xsl:template> 

    <xsl:template match="PurchaseOrderLine"> 
     <xsl:copy> 
     <Kiran> 
      <xsl:apply-templates select="@*|DocumentReference"/> 
     </Kiran> 
     <xsl:apply-templates select="@*|Item|Quantity"/> 
    </xsl:copy> 
    </xsl:template> 

    </xsl:stylesheet> 
+0

У вас не может быть двух шаблонов с 'match =" PurchaseOrderLine "'. И вы не можете создавать атрибуты после создания дочернего узла. –

+0

Как выполнить обе задачи для достижения цели? Любое предложение –

+0

В одном шаблоне у вас есть литеральный элемент результата 'Kiran', который я не вижу нигде в вашем желаемом результате, поэтому сложно сказать, чего вы хотите достичь. Просьба уточнить. –

ответ

1

Тогда я думаю, что вы хотите, чтобы шаблон выглядеть

<xsl:template match="PurchaseOrderLine"> 
    <POL> 
    <xsl:apply-templates select="@*"/> 
    <Kiran> 
     <xsl:apply-templates select="DocumentReference"/> 
    </Kiran> 
    <xsl:apply-templates select="node() except DocumentReference" /> 
    </POL> 
</xsl:template> 
Смежные вопросы