XSLT如果节点存在,则打印他选择的祖先和他自己

时间:2014-06-12 09:26:51

标签: xslt

我想通过XSLT打印C声明代码 输出,如果存在节点PARAMPOS打印他的ROOTNAME他的FUNCTIONNAME和他的PARAMNAME:

修改:输出C代码文本中Name_1(在xml中)到Name_1的转换字符“ - ”到“_”(输出必须是C语言有效,只有字母或字符可能是否可用)?

enum ParamName
{//ROOTNAME_FUNCTIONNAME_PARAMNAME
Bike_Name_1_PopA,
Bike_Name_1_PopB,
Bike_Name_2_PopX,
//last item 
SIGNforLAST
}

输出,如果存在节点PARAMPOS打印他的FUNCTIONID和他自己的PARAMPOS:

struct Parameter Parameters[SIGNforLAST - 1]
{//{FUNCTIONID,PARAMPOS},
{1, 51},
{1, 52},
{2, 72}
}

来自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>

请帮我xsl。对于文本输出,我受到converting-xml-to-plain-text-how-should-i-ignore-handle-whitespace-in-the-xslt的启发。但我的XSL是第一次尝试。

<?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 个答案:

答案 0 :(得分:0)

根据您提供的示例XML和输出。我写了下面的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>&#10;     </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>&#10;     </xsl:text></xsl:if>
</xsl:for-each>
}
</xsl:template>
</xsl:transform>

对于givene XML的输出将如下所示

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 :)