2016-11-15 9 views
0

属性の親値を別の属性の子値と連結し、特定の属性をxmlファイルから削除したいとします。私の入力XMLは次のようになります。xslを使用してxmlの親と子の属性値を連結する

<?xml version="1.0" encoding="UTF-8"?> 
<import_data> 
    <product part_number="12345" category="Parts and Accessories" description="Small-Part"> 
     <product_attribute name="organizationCode" value="XYZ"/> 
     <product_attribute name="Product Market" value="Rotors"/> 
     <product_attribute name="inventoryItemId" value="6789"> 
    </product> 
    <product part_number="ABCDE" category="Ball Bearings" description="Small-Part"> 
     <product_attribute name="organizationCode" value="XYZ"/> 
     <product_attribute name="Product Market" value="Rings"/> 
     <product_attribute name="inventoryItemId" value="FGHIJ"> 
    </product> 
</import_data> 

、出力は次のようになります。

<?xml version="1.0" encoding="UTF-8"?> 
<import_data> 
    <product part_number="12345" category="Rotors.Parts and Accessories" description="Small-Part"> 
     <product_attribute name="Product Market" value="Rotors"/> 
    </product> 
    <product part_number="ABCDE" category="Rings.Ball Bearings" description="Small-Part"> 
     <product_attribute name="Product Market" value="Rings"/> 
    </product> 
</import_data> 

私のXSLは次のようになります。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format" 
> 
<xsl:strip-space elements="*"/> 
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/> 

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


<xsl:template match="//so_product_attribute[@name='inventoryItemId']" /> 
<xsl:template match="//so_product_attribute[@name='organizationCode']" /> 

<xsl:template match="processing-instruction('xml-stylesheet')"/> 
</xsl:stylesheet> 

私は削除する方法を考え出しました(product_attribute name = "Product Market" value = "Rotors")の値を読み込み、それを親属性の値と組み合わせることはできません(producerItemIdとorganizationCode) ct category = "Parts and Accessories")は、(Product category = "Rotors.Parts and Accessories")に結合するように属性を設定します。私はxsl:for-eachを使って実験し、子の値を変数に読み込みましたが、目的の値を連結して出力​​することはできません。どんな助けでも大歓迎です。

答えて

1

は少しあなたのXSLTを微調整:product_attribute[@name = 'Product Market']@valueが非空の文字列である場合

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format" 
    > 
    <xsl:strip-space elements="*"/> 
    <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/> 

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

    <xsl:template match="product[normalize-space(product_attribute[@name = 'Product Market']/@value)]/@category"> 
    <xsl:attribute name="{name()}"> 
     <xsl:value-of select="concat(parent::product/product_attribute[@name = 'Product Market']/@value, '.', .)"/> 
    </xsl:attribute> 
    </xsl:template> 

    <xsl:template match="product_attribute[@name='inventoryItemId']" /> 
    <xsl:template match="product_attribute[@name='organizationCode']" /> 

    <xsl:template match="processing-instruction('xml-stylesheet')"/> 
</xsl:stylesheet> 
  • は、product/@categoryのためのマッチングテンプレートを追加しました。 [存在しない場合は、カテゴリの前のドットを避ける]
  • 同じ名前の新しい属性を作成し、.とコンテキストノード(現在の属性)の内容を連結します(@value)。
  • を入力要素の正しい名前に変更しました
  • 一致するパターンでは、//で始まらないようにしてください。
+0

ありがとうございました。あなたの変更を実装することができ、それは完全に機能しました。さらに、あなたのメモは、あなたのxslの背後にある推論を理解する助けになりました。 –

+0

回答を受け入れたとマークすることで質問を終了すると、私とコミュニティに役立ちます。すべての要件にお答えしたら、同意してください。 – uL1

関連する問題