2016-03-16 1 views
0

Я должен был преобразовать следующие входные XML,Переименование элемента с именем Standard в продукт с использованием XSLT

<Standards xmlns="http://ws.wso2.org/dataservice"> 
     <Standard> 
      <ProductID>200057</ProductID> 
      <Prefix>ISO</Prefix> 
      <SNumber>1001</SNumber> 
      <DraftProducts> 
       <RelatedProduct> 
        <ProductID>1500163</ProductID> 
       </RelatedProduct> 
      </DraftProducts> 
      <ReferenceProducts> 
       <RelatedProduct> 
        <ProductID>263973</ProductID> 
        <RelationId>708519</RelationId> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>320056</ProductID> 
        <RelationId>934789</RelationId> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
      </ReferenceProducts> 
      <RelatedIntProducts> 
       <RelatedProduct> 
        <ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
        <RelationId>116881</RelationId> 
        <Relationship>Identical</Relationship> 
        <Designation>NEN ISO 1001</Designation> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>208076</ProductID> 
        <RelationId>116886</RelationId> 
        <Relationship>Identical</Relationship> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
      </RelatedIntProducts> 
      <S1>1001</S1> 
      <S2>1986</S2> 
     </Standard> 
     <Standard> 
      <ProductID>200058</ProductID> 
      <Prefix>ISO</Prefix> 
      <SNumber>1002</SNumber> 
      <DraftProducts> 
       <RelatedProduct> 
        <ProductID>1500167</ProductID> 
       </RelatedProduct> 
      </DraftProducts> 
      <ReferenceProducts> 
       <RelatedProduct> 
        <ProductID>263974</ProductID> 
        <RelationId>708519</RelationId> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>320052</ProductID> 
        <RelationId>934754</RelationId> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
      </ReferenceProducts> 
      <RelatedIntProducts> 
       <RelatedProduct> 
        <ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
        <RelationId>116837</RelationId> 
        <Relationship>Identical</Relationship> 
        <Designation>NEN ISO 1001</Designation> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>208074</ProductID> 
        <RelationId>116843</RelationId> 
        <Relationship>Identical</Relationship> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
      </RelatedIntProducts> 
      <S1>1005</S1> 
      <S2>1983</S2> 
     </Standard> 
    </Standards> 

в следующем формате вывода.

<Standards xmlns="http://ws.wso2.org/dataservice"> 
     <Standard> 
      <ProductID>200057</ProductID> 
      <Prefix>ISO</Prefix> 
      <SNumber>1001</SNumber> 
      <RelatedProducts> 
       <RelatedProduct> 
        <ProductID>1500163</ProductID> 
        <RelationType>DraftProducts</RelationType> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>263973</ProductID> 
        <RelationId>708519</RelationId> 
        <RelationType>ReferenceProducts</RelationType> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>320056</ProductID> 
        <RelationId>934789</RelationId> 
        <RelationType>ReferenceProducts</RelationType> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
        <RelationId>116881</RelationId> 
        <Relationship>Identical</Relationship> 
        <RelationType>RelatedIntProducts</RelationType> 
        <Designation>NEN ISO 1001</Designation> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>208076</ProductID> 
        <RelationId>116886</RelationId> 
        <RelationType>RelatedIntProducts</RelationType> 
        <Relationship>Identical</Relationship> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
      </RelatedProducts> 
      <S1>1001</S1> 
      <S2>1986</S2> 
     </Standard> 
     <Standard> 
      <ProductID>200058</ProductID> 
      <Prefix>ISO</Prefix> 
      <SNumber>1002</SNumber> 
      <RelatedProducts> 
       <RelatedProduct> 
        <ProductID>1500167</ProductID> 
        <RelationType>DraftProducts</RelationType> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>263974</ProductID> 
        <RelationId>708519</RelationId> 
        <RelationType>ReferenceProducts</RelationType> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>320052</ProductID> 
        <RelationId>934754</RelationId> 
        <RelationType>ReferenceProducts</RelationType> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
        <RelationId>116837</RelationId> 
        <RelationType>RelatedIntProducts</RelationType> 
        <Relationship>Identical</Relationship> 
        <Designation>NEN ISO 1001</Designation> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>208074</ProductID> 
        <RelationId>116843</RelationId> 
        <RelationType>RelatedIntProducts</RelationType> 
        <Relationship>Identical</Relationship> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
      </RelatedProducts> 
      <S1>1005</S1> 
      <S2>1983</S2> 
     </Standard> 
    </Standards> 

При поддержке со стороны других в форуме я смог написать следующее XSLT сделать это преобразование.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:x="http://ws.wso2.org/dataservice" 
    xmlns="http://ws.wso2.org/dataservice" 
    exclude-result-prefixes="x" 
    version="1.0"> 

    <xsl:output indent="yes" method="xml" /> 

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

    <xsl:template match="x:Standard"> 
     <xsl:copy> 
      <xsl:copy-of select="x:ProductID"/> 
      <xsl:copy-of select="x:Prefix"/> 
      <xsl:copy-of select="x:SNumber"/> 
      <RelatedProducts> 
       <xsl:apply-templates select=".//x:RelatedProduct"/> 
      </RelatedProducts> 
      <xsl:copy-of select="x:S1"/> 
      <xsl:copy-of select="x:S2"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="x:RelatedProduct"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
      <RelationType><xsl:value-of select="name(..)"/></RelationType> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

Но теперь мне нужно переименовать все элементы, которые в настоящее время называются стандартными в продукт, а конечный результат должен быть следующим.

<Standards xmlns="http://ws.wso2.org/dataservice"> 
    <Product> 
     <ProductID>200057</ProductID> 
     <Prefix>ISO</Prefix> 
     <SNumber>1001</SNumber> 
     <RelatedProducts> 
      <RelatedProduct> 
       <ProductID>1500163</ProductID> 
       <RelationType>DraftProducts</RelationType> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>263973</ProductID> 
       <RelationId>708519</RelationId> 
       <RelationType>ReferenceProducts</RelationType> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>320056</ProductID> 
       <RelationId>934789</RelationId> 
       <RelationType>ReferenceProducts</RelationType> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       <RelationId>116881</RelationId> 
       <Relationship>Identical</Relationship> 
       <RelationType>RelatedIntProducts</RelationType> 
       <Designation>NEN ISO 1001</Designation> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>208076</ProductID> 
       <RelationId>116886</RelationId> 
       <RelationType>RelatedIntProducts</RelationType> 
       <Relationship>Identical</Relationship> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
     </RelatedProducts> 
     <S1>1001</S1> 
     <S2>1986</S2> 
    </Product> 
    <Product> 
     <ProductID>200058</ProductID> 
     <Prefix>ISO</Prefix> 
     <SNumber>1002</SNumber> 
     <RelatedProducts> 
      <RelatedProduct> 
       <ProductID>1500167</ProductID> 
       <RelationType>DraftProducts</RelationType> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>263974</ProductID> 
       <RelationId>708519</RelationId> 
       <RelationType>ReferenceProducts</RelationType> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>320052</ProductID> 
       <RelationId>934754</RelationId> 
       <RelationType>ReferenceProducts</RelationType> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       <RelationId>116837</RelationId> 
       <RelationType>RelatedIntProducts</RelationType> 
       <Relationship>Identical</Relationship> 
       <Designation>NEN ISO 1001</Designation> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>208074</ProductID> 
       <RelationId>116843</RelationId> 
       <RelationType>RelatedIntProducts</RelationType> 
       <Relationship>Identical</Relationship> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
     </RelatedProducts> 
     <S1>1005</S1> 
     <S2>1983</S2> 
    </Product> 
</Standards> 

Я попробовал несколько подходов и изменил приведенный выше xslt, но все равно не повезло. Я новичок в xslt. Как я могу изменить вышеупомянутый xslt, чтобы сделать это. Любая помощь будет оценена по достоинству.

ответ

1

Просто замените <xsl:copy> в

<xsl:template match="x:Standard"> 
       <xsl:copy> 
        <xsl:copy-of select="x:ProductID" /> 

с <Product> (и закрывающим тегом, тоже, конечно). Это так просто, не изменяя что-то другое, давая

<xsl:template match="x:Standard"> 
       <Product> 
        <xsl:copy-of select="x:ProductID" /> 

Результат будет:

<?xml version="1.0"?> 
<Standards xmlns="http://ws.wso2.org/dataservice"> 
    <Product> 
     <ProductID>200057</ProductID> 
     <Prefix>ISO</Prefix> 
     <SNumber>1001</SNumber> 
     <RelatedProducts> 
      <RelatedProduct> 
       <ProductID>1500163</ProductID> 
       <RelationType>DraftProducts</RelationType> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>263973</ProductID> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       <RelationType>ReferenceProducts</RelationType> 
      </RelatedProduct> 
      ... 

Если вы также хотите скопировать RelationId просто удалить exeception из последнего шаблона, так что это выглядит следующим образом:

<xsl:template match="x:RelatedProduct"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|*"/> 
    <RelationType><xsl:value-of select="name(..)"/></RelationType> 
    </xsl:copy> 
</xsl:template> 

Применение обоих дает точный результат.

+0

Я пробовал это, но это не дает ожидаемого результата. –

+0

Я сравнивал оба результата и за исключением 'RelationID', они были идентичны. Тем не менее, я обновил свой ответ, связанный с этим. – zx485

+0

Жаль, что это была моя ошибка, ваше решение, похоже, работает. Вот мой голос. Большое спасибо. :) –