XSLT Si existe un nodo, imprima sus antepasados seleccionados y él mismo
Frecuentes
Visto 135 equipos
1
I want print C declaration code by XSLT.
Output,if exist node PARAMPOS print his ROOTNAME his FUNCTIONNAME and his PARAMNAME:
MODIFICATION: HOW TRANSFORM CHARACTER "-" TO "_" (OUTPUT MUST BE C LANGUAGE VALID, ONLY ALPHANUMERIC OR UNDERSCORE CHARACTER IS POSSIBLE) in Name-1 (in xml) to Name_1 in output C code text?
enum ParamName
{//ROOTNAME_FUNCTIONNAME_PARAMNAME
Bike_Name_1_PopA,
Bike_Name_1_PopB,
Bike_Name_2_PopX,
//last item
SIGNforLAST
}
Output,if exist node PARAMPOS print his FUNCTIONID and the himself PARAMPOS:
struct Parameter Parameters[SIGNforLAST - 1]
{//{FUNCTIONID,PARAMPOS},
{1, 51},
{1, 52},
{2, 72}
}
desde XML
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<ROOTNAME>Bike</ROOTNAME>
<Function>
<FunctionID>1</FunctionID>
<FunctionName>Name-1</FunctionName>
<FunctionClass>
<PUParam>
<ParamName>PopA</ParamName>
<PUParamOPType>
<ParamPos>51</ParamPos>
</PUParamOPType>
</PUParam>
<PUParam>
<ParamName>PopB</ParamName>
<PUParamOPType>
<ParamPos>52</ParamPos>
</PUParamOPType>
</PUParam>
<PUParam>
<ParamName>PopC-without</ParamName>
<PUParamOPType>
empty
</PUParamOPType>
</PUParam>
</FunctionClass>
</Function>
<Function>
<FunctionID>2</FunctionID>
<FunctionName>Name-2</FunctionName>
<FunctionClass>
<PUParam>
<ParamName>PopX</ParamName>
<PUParamOPType>
<ParamPos>72</ParamPos>
</PUParamOPType>
</PUParam>
</FunctionClass>
</Function>
</ROOT>
Please help me with xsl. For text output I inspired at converting-xml-to-plain-text-how-should-i-ignore-handle-whitespace-in-the-xslt. But my XSL is first attempt.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xsl:transform [
<!ENTITY s "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> </xsl:text>" >
<!ENTITY s2 "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> </xsl:text>" >
<!ENTITY s4 "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> </xsl:text>" >
<!ENTITY s6 "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> </xsl:text>" >
<!ENTITY e "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'></xsl:text>" >
<!ENTITY n "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
</xsl:text>" >
]>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="ROOTNAME">
<xsl:for-each select="Function">
<xsl:value-of select="FunctionID"/>&s2;<xsl:value-of select="FunctionName"/>&s2;
<xsl:value-of select="FunctionClass/PUParam/ParamName"/>&n;
</xsl:for-each>
</xsl:template>
</xsl:transform>
1 Respuestas
0
As per your given sample XML and output. I have written below XSLT
<xsl:transform version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="ROOTNAME">
<xsl:value-of select="/ROOT/ROOTNAME"/>
</xsl:variable>
enum ParamName
{
<xsl:variable name="temp1">
<xsl:for-each select="/ROOT/Function">
<xsl:variable name="funcName" select="FunctionName"></xsl:variable>
<xsl:for-each select="./FunctionClass/PUParam[./PUParamOPType/ParamPos]">
<temp><xsl:value-of select="concat($ROOTNAME,'_',$funcName,'_',./ParamName)"/> </temp>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="$temp1/temp">
<xsl:value-of select="."/><xsl:if test="position()!=last()">,<xsl:text> </xsl:text></xsl:if>
</xsl:for-each>
SIGNforLAST
}
struct Parameter Parameters[SIGNforLAST - 1]
{
<xsl:variable name="temp2">
<xsl:for-each select="/ROOT/Function">
<xsl:variable name="funcId" select="FunctionID"></xsl:variable>
<xsl:for-each select="./FunctionClass/PUParam[./PUParamOPType/ParamPos]">
<temp>
<xsl:value-of select="concat('{',$funcId,',',./PUParamOPType/ParamPos,'}')"/>
</temp>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="$temp2/temp">
<xsl:value-of select="."/><xsl:if test="position()!=last()">,<xsl:text> </xsl:text></xsl:if>
</xsl:for-each>
}
</xsl:template>
</xsl:transform>
Out put for givene XML will be as below
enum ParamName
{
Bike_Name1_PopA,
Bike_Name1_PopB,
Bike_Name2_PopX
SIGNforLAST
}
struct Parameter Parameters[SIGNforLAST - 1]
{
{1,51},
{1,52},
{2,72}
}
Please update the output as per your requirement. Thanks :)
Respondido el 12 de junio de 14 a las 16:06
Your XSL code return me in Notepad++ XMLToolsPlugin ver2.3.2 only text enum ParamName { (no jump into <xsl:variable name="temp1">). In Firefox there was error message: Error during XSLT transformation: An XPath expression should return a set of nodes. What kind of tool can I use? What tool for debug XSL?Thanks - matejhyn
But in XMLSpy2013 function it well. Thanks! WHY THE XMLToolPlugin in Notapad++ DON'T EXECUTED THIS? - matejhyn
XMSpy supports 1.1 version. XMLToolsPlugin ver2.3.2 maynot support version 1.1. I am assigning to node to variable which is not support by XSLT 1.0. Thats why I used 1.1. I have captured complete node to variable to add ',' and new line other than last node. - Isaac G Sivaa
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas xslt or haz tu propia pregunta.
Please post the XSLT you have tried. - helderdarocha