2010-08-08 2 views

답변

0

짧은 솔루션. 이 스타일 :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="@*|node()" name="identity"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="Configuration/*[1]"> 
     <Parameter1> 
      <send>0</send> 
      <interval>0</interval> 
      <speed>200</speed> 
     </Parameter1> 
     <xsl:call-template name="identity" /> 
    </xsl:template> 
</xsl:stylesheet> 

출력 :

<Configuration> 
    <Parameter1> 
     <send>0</send> 
     <interval>0</interval> 
     <speed>200</speed> 
    </Parameter1> 
    <Parameter2></Parameter2> 
</Configuration> 

: Configuration/*[1][not(/Configuration/Parameter1)] : 어떤 위치에서 이러한 요소가 이미없는 경우에만이 Parameter1을 추가 할 경우의 패턴을 변경해야합니다

3

이 XSLT는 Parameter2 전에 Configuration 요소의 자식으로 지정된 내용을 삽입합니다.

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

    <xsl:template match="Configuration"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*" /> 
      <!--Check for the presence of Parameter1 in the config file to ensure that it does not get re-inserted if this XSLT is executed against the output--> 
      <xsl:if test="not(Parameter1)"> 
       <Parameter1> 
        <send>0</send> 
        <interval>0</interval> 
        <speed>200</speed> 
       </Parameter1> 
      </xsl:if> 
      <!--apply templates to children, which will copy forward Parameter2 (and Parameter1, if it already exists)--> 
      <xsl:apply-templates select="node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <!--standard identity template, which copies all content forward--> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+0

저는 아이들을위한 복사본을 만드는 재귀 템플릿보다는 복사본을 사용했을 것이라고 생각합니다. 그러나 그래야만합니다. 기존 Parameter1에 대해 방어 할 수 있다면 두 번 투표 할 것입니다. –

관련 문제