2017-08-22 2 views
0

Ich möchte wissen, wie der XML-Tag-Wert beibehalten wird, wenn das Tag mit XSLT umbenannt wird. Unten ist mein xml, xslt und das gewünschte Ergebnis. Ich bin nicht sicher, wie dies zu tun ist, also habe ich versucht, dies mit Variablen und auch wählen, wann und sonst in XLST Tags.So behalten Sie den XML-Tag-Wert bei, wenn Sie das Tag mit XSLT umbenennen

Die XML:

<?xml version="1.0" encoding="UTF-8"?> 
<x> 
    <y> 
     <z value="john" designation="manager"> 
      <z value="mike" designation="associate"></z> 
      <z value="dave" designation="associate"></z> 
     </z> 
    </y> 
</x> 

Die XSLT:

<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="*"/> 

<xsl:variable name="var"> 
name="manager value=\"sam\"" 
</xsl:variable> 

<xsl:template match="x"> 
    <employees> 
     <xsl:apply-templates /> 
    </employees> 
</xsl:template> 

<xsl:template match="y"> 
    <employee> 
     <xsl:apply-templates /> 
    </employee> 
</xsl:template> 

<xsl:template match="z"> 
<xsl:choose> 
    <xsl:when test="sam"> 
    <xsl:element name="{$var}"> 
    </xsl:element> 
    </xsl:when> 
    <xsl:otherwise> 
    <xsl:element name="{@designation}"> 
     <xsl:value-of select="@value"/> 
     <xsl:apply-templates /> 
    </xsl:element> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

</xsl:stylesheet> 

Die gewünschte Ausgabe:

<?xml version="1.0" encoding="UTF-8"?> 
<employees> 
    <employee> 
     <manager value="john"> 
      <associate>mike</associate> 
      <associate>dave</associate> 
     </manager> 
    </employee> 
</employees> 
+0

Die Beispiel ist mehrdeutig - bitte erläutern Sie die erforderliche Logik in Worten. –

Antwort

0

Dies sollte funktionieren:

<?xml version="1.0" encoding="UTF-8"?> 
<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:template match="x"> 
     <employees> 
      <xsl:apply-templates/> 
     </employees> 
    </xsl:template> 

    <xsl:template match="y"> 
     <employee> 
      <xsl:apply-templates/> 
     </employee> 
    </xsl:template> 

    <xsl:template match="*[contains(@designation, 'manager')]"> 
     <manager> 
      <xsl:attribute name="value"><xsl:value-of select="@value"/></xsl:attribute> 
      <xsl:apply-templates/> 
     </manager> 
    </xsl:template> 

    <xsl:template match="*[contains(@designation, 'associate')]"> 
     <associate> 
      <xsl:value-of select="@value"/> 
     </associate> 
    </xsl:template> 

</xsl:stylesheet> 
Verwandte Themen