2017-11-23 2 views
1

名前をハードコードせずに、単一のノードに複数の属性を追加しようとしています。XSLT:複数の属性を追加する

:私のXSLT(バージョン1.0)はこれまでのところ、このようになります

<xml> 
    <Item Name="FooName1" 
      ID="1000" 
      Description="FooDescription1" 
      > 
     <BOM> 
      <Child Name="FooName2" 
      ID="2000" 
      Description="FooDescription2" 
      /> 
      <Child Name="FooName3" 
      ID="3000" 
      Description="FooDescription3" 
      /> 
     </BOM> 
    </Item> 
</xml> 

:私はこのような何かを見て出力XMLを希望

<bom> 
    <columns> 
     <column id="0">Name</column> 
     <column id="1">ID</column> 
     <column id="2">Description</column> 
    </columns> 
    <rows> 
     <row number="0" level="0" position=""> 
      <cell column="0">FooName1</cell> 
      <cell column="1">1000</cell> 
      <cell column="2">FooDescription1</cell> 
     </row> 
     <row number="1" level="1" position=""> 
      <cell column="0">FooName2</cell> 
      <cell column="1">2000</cell> 
      <cell column="2">FooDescription2</cell> 
     </row> 
     <row number="2" level="1" position=""> 
      <cell column="0">FooName3</cell> 
      <cell column="1">3000</cell> 
      <cell column="2">FooDescription3</cell> 
     </row> 
    </rows> 
</bom> 

入力XMLは次のようになります

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output method="xml" version="1.0" indent="yes" encoding="utf-8"/> 

    <xsl:template match="bom"> 
    <impxml> 
     <Item> 
      <xsl:apply-templates select="columns/column" /> 
     </Item> 
    </impxml> 
    </xsl:template> 

    <xsl:template match="columns/column"> 
    <!-- <xsl:value-of select="."/> Fetch all column values --> 
    <xsl:for-each select="."> 
     <xsl:attribute name="TEST">TEST</xsl:attribute> 
    </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

<xsl:attribute name="">は、1つのハードコードされた名前のみを受け入れます。 columns \ columnのすべての値を反復処理して、それらを属性として同じItemまたはChildノードに追加するにはどうすればよいですか?

私はXSLTの新機能ですから、ここではどんな入力/ヘルプもありがとうございます。ありがとうございました。

+0

をあなたは 'てみました<のxsl:属性名= ""/>xsl:variable使用@id@column値を対応することで一致していますb "/>'? – Yunnosch

+0

属性名 'ItemNo'は2つの' child'要素のどこから来ますか? –

+0

@TimC:混乱して申し訳ありません、それは 'Description'だったはずです。 「ItemNo」はタイプミスでした。私はこれを修正しました。ありがとうございます。 – georgekw

答えて

0

ancestor::*を使用して、上部に<column>の値を取得することを検討してください。次に、属性外の<xsl:value-of select="path" />に相当する動的属性名に{path}を使用します。 「=属性名:

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output method="xml" version="1.0" indent="yes" encoding="utf-8"/> 

    <xsl:template match="bom"> 
    <impxml> 
      <xsl:apply-templates select="rows/row" /> 
    </impxml> 
    </xsl:template> 

    <xsl:template match="rows/row"> 
     <Item> 
      <xsl:apply-templates select="cell" /> 
     </Item> 
    </xsl:template> 

    <xsl:template match="cell"> 
    <xsl:variable name="curr_col"><xsl:value-of select="@column"/></xsl:variable>  
     <xsl:attribute name="{ancestor::bom/columns/column[@id=$curr_col]}"> 
      <xsl:value-of select="."/> 
     </xsl:attribute> 
    </xsl:template> 

</xsl:stylesheet> 

出力

<impxml> 
    <Item Name="FooName1" ID="1000" Description="FooDescription1"/> 
    <Item Name="FooName2" ID="2000" Description="FooDescription2"/> 
    <Item Name="FooName3" ID="3000" Description="FooDescription3"/> 
</impxml> 
+0

華麗な、ありがとう!これはまさに私が探していたものでした。 – georgekw

+0

優秀!お役に立てて嬉しいです。 – Parfait

関連する問題