2017-03-08 6 views
0

노드를 복사하고 속성을 변경하려고했습니다.적용된 템플릿이 여러 조건 자와 규칙이 일치하지 않습니다.

<container> 
    <parent id="1"> 
    <child id="1"/> 
    <child id="2"/> 
    </parent> 
    <parent id="2"> 
    <child id="1"/> 
    <child id="2"/> 
    <child id="3" attrs="I am special"/> 
    </parent> 
</container> 

결과 문서는 다음과 같이 보일 것이다 :

<container> 
    <parent id="1"> 
    <child id="1"/> 
    <child id="2"/> 
    <child id="3" attrs="I am special" cloned="I have been cloned"/> 
    </parent> 
    <parent id="2"> 
    <child id="1"/> 
    <child id="2"/> 
    <child id="3" attrs="I am special"/> 
    </parent> 
</container> 

을 아이를 복사하려면 나는 단순히 기본적으로, <parent id="1"><child id="3">을 가지고 있으며이 문서에, 그것에 속성을 추가 할 마찬가지로, 자신의 욕망의 대상과 내가 채우려 parentapply-templates를 선택

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" indent="yes"/> 

    <!-- copy everything verbatim --> 
    <xsl:template match="@*|node()" name="identity"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="parent[@id='1']/child[last()]"> 
    <xsl:call-template name="identity"/> 
    <xsl:apply-templates select="//*/child[@id='3']"/> 
    </xsl:template> 
</xsl:stylesheet> 

를 복사 탁으로 신경 써서, 나는 속성을 바꾸려고 노력했지만 아무 소용이 없다. 아무것도 이제까지 일치되지 않으며, 속성은 다음과 같은 템플릿으로 추가되지 않습니다 : XSLT의 내 제한된 이해를

<xsl:template match="child[@id='3' and ../@id='1']"> 
    <xsl:apply-templates select="@*|node()" /> 
    <xsl:attribute name="clone">I have been cloned</xsl:attribute> 
</xsl:template> 

을, 이것은 apply-templates 동안 트리거 된 것이다 노드를 복사하여 내 속성을 추가 한 것 . 그렇지 않으면 출력에 child id="3"이 복사되지만 속성의 부호는 표시되지 않습니다.

새로 추가 된 노드가 "액세스 가능"하지 않았거나 그런 식으로 생각할 수도 있지만 간단한 규칙이이 규칙과 일치합니다 (원래 복사 한 노드를 수정하기 때문에 좋지 않습니다) :

나는 또한 나의 "사본을 생각

<xsl:template match="child[@id='1' and ../@id='1']"> 
    <xsl:copy> 
    <xsl:attribute name="clone">Nope, just a normal kid</xsl:attribute> 
    <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
</xsl:template> 

:

<xsl:template match="child[@id='3']"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()" /> 
    <xsl:attribute name="att1">I have been cloned?!</xsl:attribute> 
    </xsl:copy> 
</xsl:template> 

은 내가 술어를 엉망으로하지만, 마법처럼 비슷한 조건 (마이너스 이전 사본)과 속성을 추가 작동했다되었을 수 있습니다 생각 "템플릿이 더 높은 우선 순위와 일치했을 수도 있지만 g 우선 순위도 도움이되지 않았다.

내가 누락 된 항목이 있습니까?

답변

1

새 요소는 결과 트리의 일부이므로 동일한 변환 단계에서 일치시킬 수 없습니다. 나는 요소의 처리가 추가 될 변경할 수있는 모드를 사용하도록 제안 :

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

    <xsl:template match="@*|node()" mode="#all"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" mode="#current"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="parent[@id='1']/child[last()]"> 
    <xsl:next-match/> 
    <xsl:apply-templates select="//*/child[@id='3']" mode="add-att"/> 
    </xsl:template> 

<xsl:template match="child[@id='3']" mode="add-att"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*"/> 
    <xsl:attribute name="att1">I have been cloned?!</xsl:attribute> 
    <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 

</xsl:transform> 

온라인 http://xsltransform.net/ncntCTd에서.