2016-08-09 61 views
0

Ich habe eine XML wie:Wie nach Zustand die Werte von XML ändern XSLT mit

<all> 
    <one>Something 1</one> 
    <two>something 2</two> 
    <check> 
     <present>true</present> 
    </check> 
    <action> 
     <perform></perform> 
    </action> 
</all> 

Ich möchte XSL XML Transformation durchführen mit:

erwartete Ausgabe: wenn <present>true</present>

all> 
    <one>Something 1</one> 
    <two>something 2</two> 
    <check> 
     <present>YES</present> 
    </check> 
    <action> 
     <perform>READ</perform> 
    </action> 
</all> 

else if: <present>false</present>

<all> 
    <one>Something 1</one> 
    <two>something 2</two> 
    <check> 
     <present>NO</present> 
    </check> 
    <action> 
     <perform>INSERT</perform> 
    </action> 
</all> 

Ist es möglich zu tun? Ich bin nicht bewusst über Zustand XSL bei der Prüfung ich versuche, das Element zu bewegen, aber funktionierte nicht:

<xsl:template match="perform"> 
    <xsl:copy> 
    <xsl:choose> 
     <xsl:when test="../check/present = 'true'"> 
     <xsl:text>READ</xsl:text> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:apply-templates/> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:copy> 
</xsl:template> 

Antwort

1

Warum nicht Sie tun genau das, was Sie sagen, getan werden muss:

<xsl:template match="perform"> 
    <xsl:copy> 
     <xsl:choose> 
      <xsl:when test="../../check/present='true'">READ</xsl:when> 
      <xsl:when test="../../check/present='false'">INSERT</xsl:when> 
     </xsl:choose> 
    </xsl:copy> 
</xsl:template> 
+1

Vielen Dank. Es hat funktioniert –

0

Versuchen Sie dies:

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

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

<xsl:template match="action"/> 

<xsl:template match="check/present"> 
    <xsl:choose> 
      <xsl:when test=".='true'"> 
       <xsl:copy><xsl:text>YES</xsl:text></xsl:copy> 
       <action> 
        <perform><xsl:text>READ</xsl:text></perform> 
       </action> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:copy><xsl:text>NO</xsl:text></xsl:copy> 
       <action> 
        <perform><xsl:text>INSERT</xsl:text></perform> 
       </action> 
      </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 
+1

Danke für die Antwort .. –

+1

Ich habe noch eine Sorge. Wollte nur wissen, ist es möglich, die xsl-Datei in git Repo und lesen Sie ihre Inhalte auf lokale und führen Sie die Transformation –