2017-03-03 9 views
1

XSLT!動的に作成された兄弟要素の属性にアクセスする

<Chapter> 
    <Content> 
     <Ref id="INT1" Function="CONDUCTOR">INT1</Ref> 
     <Ref id="INT2" Function="SIGNAL">INT2</Ref> 
     <Ref id="INT3" Function="MIXED">INT3</Ref> 
     <Ref id="INT4" Function="PLANE">INT4</Ref> 
     <Ref id="INT5" Function="CORE">INT5</Ref> 
    </Content> 
</Chapter> 
:変換するためにサクソンとXSLT 2.0を使用して

...

<?xml version = "1.0" encoding = "UTF-8"?> 
    <Chapter revision="B" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <Content Ref="X123"> 
      <Ref name="INT1"/> 
      <Ref name="INT2"/> 
      <Ref name="INT3"/> 
      <Ref name="INT4"/> 
      <Ref name="INT5"/> 
     </Content> 
     <Data> 
      <Reference name="INT1" Function="CONDUCTOR"></Reference> 
      <Reference name="INT2" Function="SIGNAL"></Reference> 
      <Reference name="INT3" Function="MIXED"></Reference> 
      <Reference name="INT4" Function="PLANE"></Reference> 
      <Reference name="INT5" Function="CORE"></Reference> 
     </Data> 
    </Chapter> 

が、私はそれがこれを生成します:

は、私はこのようなXMLソースツリーフラグメントを持っています

私のテンプレート断片は次のとおりです。

<xsl:template match="/"> 
    <xsl:apply-templates select="/Chapter"/> 
</xsl:template> 

<xsl:template match="/Chapter"> 
    <xsl:element name="{name()}"> 
     <xsl:apply-templates select="Content"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="Content"> 
    <xsl:element name="{name()}"> 
     <xsl:apply-templates select="Ref"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="Ref"> 
    <xsl:element name="{name()}"> 

     <xsl:attribute name="id"> 
      <xsl:value-of select="@name"/> 
     </xsl:attribute> 

     <xsl:attribute name="Function"> 
      <xsl:value-of select="/Chapter/Data/Reference[@name=/Chapter/Data/Reference/@name]/@Function"/> 
     </xsl:attribute> 

     <xsl:value-of select="@name"/> 

    </xsl:element> 
</xsl:template> 

しかし、上記のテンプレートは、この生成します それは明らかに私は、属性値をステップ実行する述語値として供給する必要がありますどのようなすべてのノード

<Chapter> 
     <Content> 
      <Ref id="INT1" Function="CONDUCTOR SIGNAL MIXED PLANE CORE">INT1</Ref> 
      <Ref id="INT2" Function="CONDUCTOR SIGNAL MIXED PLANE CORE">INT2</Ref> 
      <Ref id="INT3" Function="CONDUCTOR SIGNAL MIXED PLANE CORE">INT3</Ref> 
      <Ref id="INT4" Function="CONDUCTOR SIGNAL MIXED PLANE CORE">INT4</Ref> 
      <Ref id="INT5" Function="CONDUCTOR SIGNAL MIXED PLANE CORE">INT5</Ref> 
     </Content> 
    </Chapter> 

から値を拾っているの?

感謝

答えて

0

変更/Chapter/Data/Reference[@name=/Chapter/Data/Reference/@name]/@Function/Chapter/Data/Reference[@name=current()/@name]/@Functionには、その後、キーについて学び、<xsl:key name="ref" match="Data/Reference" use="@name"/>を定義し、パフォーマンスと読みやすさを改善するために、代わりにkey('ref', @name)/@Functionを使用しています。一般的に

、多くの

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 

    <xsl:key name="ref" match="Data/Reference" use="@name"/> 

    <xsl:template match="/Chapter"> 
     <xsl:copy copy-namespaces="no"> 
      <xsl:apply-templates select="Content"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Content"> 
     <xsl:copy copy-namespaces="no"> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Ref"> 
     <Ref id="{@name}" Function="{key('ref', @name)/@Function}"> 
      <xsl:value-of select="@name"/> 
     </Ref> 
    </xsl:template> 

</xsl:stylesheet> 
+0

マーティンを

<xsl:template match="Ref"> <xsl:element name="{name()}"> <xsl:attribute name="id"> <xsl:value-of select="@name"/> </xsl:attribute> <xsl:attribute name="Function"> <xsl:value-of select="/Chapter/Data/Reference[@name=/Chapter/Data/Reference/@name]/@Function"/> </xsl:attribute> <xsl:value-of select="@name"/> </xsl:element> </xsl:template> 

<xsl:template match="Ref"> <Ref id="{@name}" Function="{key('ref', @name)/@Function}"> <xsl:value-of select="@name"/> </Ref> </xsl:template> 

になり、完全なコードがあるように、あなたはリテラル結果要素や属性値テンプレートについて学びたいと思うかもしれませんあなたのソリューションに感謝します。美しく動作します。そして、あなたの他の提案や、私が追加の研究分野を指導してくれて、ありがとう。あなたの時間とトラブルにとても感謝しています! Ralph –

+0

私はあなたの提案を使用しました:/ Chapter/Data/Reference [@ name = current()/ @ name]/@ Function –

+0

マーティン、あなたの提案を /Chapter/Data/Reference [@ name = current( )/ @ name]/@ Function これは問題なく動作します。前の名前に関連付けられた関数をどのように取得するのですか?私は述語に [@ name = preceding-sibling :: * [1]/@ name] を使ってみましたが、何も返しません。 current()は先行兄弟と同じコンテキストを持っていますか? Ralph B –

関連する問題