2016-03-12 1 views
1

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

<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, но до сих пор не дает как ожидалось. Данный xslt работает только при наличии одного стандарта. Но с несколькими стандартами он дает дублированные результаты из-за шаблона xsl, который я использовал здесь.

<?xml version="1.0" encoding="UTF-8"?> 

<!-- When matching RelatedIntProducts: do nothing --> 
<xsl:template match="//x:Standards/x:Standard/x:DraftProducts" /> 
<xsl:template match="//x:Standards/x:Standard/x:RelatedIntProducts" /> 
<xsl:template match="//x:Standards/x:Standard/x:SupersdProducts" /> 
<xsl:template match="//x:Standards/x:Standard/x:LinkProducts" /> 


    <xsl:template match="//x:Standards/x:Standard/x:ReferenceProducts"> 
     <RelatedProducts> 
      <xsl:for-each select="//x:Standards/x:Standard/*/x:RelatedProduct"> 
       <RelatedProduct> 
        <ProductID> 
         <xsl:value-of select="x:ProductID"> 
         </xsl:value-of> 
        </ProductID> 
        <RelationId> 
         <xsl:value-of select="x:RelationId"> 
         </xsl:value-of> 
        </RelationId> 

        <RelationType> 
         <xsl:value-of select="name(..)" /> 
        </RelationType> 
       </RelatedProduct> 
      </xsl:for-each> 
     </RelatedProducts> 
    </xsl:template> 

</xsl:stylesheet> 

Для каждого стандарта, мне нужно, чтобы объединить все различные типы RelatedProducts в один список именованных RelatedProducts. Также мне нужно держать другие вещи такими, какие есть. Не могли бы вы помочь мне в этом.

ответ

2

Переключение с императивной обработки «pull» (<xsl:for-each...>) на «push» обработку, где вы зависите от естественного цикла обработки XSL-механизма, значительно упрощает таблицу стилей.

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

Это, по существу, «тождественное преобразование» плюс один шаблон, чтобы соответствовать Standard, и выход только то, что мы хотим от внутри него. Шаблон для x:RelatedProduct копирует эти элементы в вывод дословно и добавляет элемент RelationType, текст которого является именем содержащего элемента.

Выход

<?xml version="1.0" encoding="UTF-8"?> 
<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> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      <RelationType>ReferenceProducts</RelationType> 
     </RelatedProduct> 
     <RelatedProduct> 
       <ProductID>320056</ProductID> 
       <RelationId>934789</RelationId> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      <RelationType>ReferenceProducts</RelationType> 
     </RelatedProduct> 
     <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> 
      <RelationType>RelatedIntProducts</RelationType> 
     </RelatedProduct> 
     <RelatedProduct> 
       <ProductID>208076</ProductID> 
       <RelationId>116886</RelationId> 
       <Relationship>Identical</Relationship> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      <RelationType>RelatedIntProducts</RelationType> 
     </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> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      <RelationType>ReferenceProducts</RelationType> 
     </RelatedProduct> 
     <RelatedProduct> 
       <ProductID>320052</ProductID> 
       <RelationId>934754</RelationId> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      <RelationType>ReferenceProducts</RelationType> 
     </RelatedProduct> 
     <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> 
      <RelationType>RelatedIntProducts</RelationType> 
     </RelatedProduct> 
     <RelatedProduct> 
       <ProductID>208074</ProductID> 
       <RelationId>116843</RelationId> 
       <Relationship>Identical</Relationship> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      <RelationType>RelatedIntProducts</RelationType> 
     </RelatedProduct> 
     </RelatedProducts> 
     <S1>1005</S1> 
     <S2>1983</S2> 
    </Standard> 
</Standards> 
+0

Привет Джим, спасибо за элегантный ответ. Вы спасли меня ! :) –

+0

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

+0

Я могу ответить на ваш вопрос, но сначала, пожалуйста, удалите свой «ответ», который на самом деле является еще одним вопросом. В ответах StackOverflow не задаются дополнительные вопросы. –