如何更改元素名称

时间:2016-03-17 08:01:22

标签: xml xslt

我有要求,但我需要使用XSLT。但是,我不熟悉XSLT。我找到了许多关于如何更改元素名称的解决方案,但我没有看到任何类似于我的要求的参考文献。

要求:我需要更改元素名称 <FormDataElement>

  • 如果值包含NUMERIC,则元素标记将变为 <NumericField>
  • 如果值包含TEXT,则元素标记将变为 <TextField>
  • 如果值包含CHOICE,则元素标记将变为 <Choice>

输出文件:

<eCDFDeclarations xmlns:ecdf="http://www.ctie.etat.lu/2011/ecdf">
<FileReference>000000B00000000T00000000</FileReference>
<eCDFFileVersion>aaaa</eCDFFileVersion>
<Interface>a</Interface>
<Agent>
    <MatrNbr>001</MatrNbr>
    <RCSNbr>2436</RCSNbr>
    <VATNbr>LU1223434235</VATNbr>
</Agent>
<Declarations>
    <Declarer>
    <MatrNbr>001</MatrNbr>
    <RCSNbr>2436</RCSNbr>
    <VATNbr>LU1223434235</VATNbr>
        <Declaration language="FR" type="AB" model="DIDL">
            <Year>2000</Year>
            <Period>0</Period>
            <FormData>
                <FormDataElement id="233">1NUMERIC</FormDataElement>
                <FormDataElement id="234">1NUMERIC</FormDataElement>
                <FormDataElement id="235">31TEXT</FormDataElement>
                <FormDataElement id="236">12TEXT</FormDataElement>
                <FormDataElement id="012">5010,00NUMERIC</FormDataElement>
                <FormDataElement id="204">1CHOICE</FormDataElement>
                <FormDataElement id="205">0CHOICE</FormDataElement>
                <FormDataElement id="414">90,00NUMERIC</FormDataElement>
                <FormDataElement id="415">300,00NUMERIC</FormDataElement>
                <Table>
                    <Line num="2">
                        <FormDataElement id="01">01/01/2012NUMERIC</FormDataElement>
                        <FormDataElement id="02">31/12/2012NUMERIC</FormDataElement>
                        <FormDataElement id="03">EURTEXT</FormDataElement>
                        <FormDataElement id="639">54606,08TEXT</FormDataElement>
                        <FormDataElement id="640">0CHOICE</FormDataElement>
                    </Line>
                </Table>
            </FormData>
        </Declaration>
    </Declarer>
</Declarations>

预期输出

<ecdf:eCDFDeclarations xmlns:ecdf="http://www.ctie.etat.lu/2011/ecdf">
<ecdf:FileReference>000000B00000000T00000000</ecdf:FileReference>
<ecdf:eCDFFileVersion>aaaa</ecdf:eCDFFileVersion>
<ecdf:Interface>a</ecdf:Interface>
<ecdf:Agent>
    <ecdf:MatrNbr>String</ecdf:MatrNbr>
    <ecdf:RCSNbr>String</ecdf:RCSNbr>
    <ecdf:VATNbr>String</ecdf:VATNbr>
</ecdf:Agent>
<ecdf:Declarations>
    <ecdf:Declarer>
        <ecdf:MatrNbr>String</ecdf:MatrNbr>
        <ecdf:RCSNbr>String</ecdf:RCSNbr>
        <ecdf:VATNbr>String</ecdf:VATNbr>
        <ecdf:Declaration language="FR" type="aaaaaaaaaaaaaaaaaaaa" model="aaaaaaaaaaaaaaaaaaaa">
            <ecdf:Year>2000</ecdf:Year>
            <ecdf:Period>0</ecdf:Period>
            <ecdf:FormData>
                <NumericField id="233">1</NumericField>
                <NumericField id="234">1</NumericField>
                <TextField id="235">31</TextField>
                <TextField id="236">12</TextField>
                <NumericField id="012">5010,00</NumericField>
                <Choice id="204">1</Choice>
                <Choice id="205">0</Choice>
                <NumericField id="414">90,00</NumericField>
                <NumericField id="415">300,00</NumericField>
                <ecdf:Table>
                    <ecdf:Line num="2">
                        <NumericField id="01">01/01/2012</NumericField>
                        <NumericField id="02">31/12/2012</NumericField>
                        <TextField id="03">EUR</TextField>
                        <TextField id="639">54606,08</TextField>
                        <Choice id="640">0</Choice>
                    </ecdf:Line>
                </ecdf:Table>
            </ecdf:FormData>
        </ecdf:Declaration>
    </ecdf:Declarer>
</ecdf:Declarations>

提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

<xsl:template match="FormDataElement">
<xsl:variable name="outputElementName">
  <xsl:choose>
    <xsl:when test="contains(., 'NUMERIC')">NumericField</xsl:when>
    <xsl:when test="contains(., 'TEXT')">TextField</xsl:when>
    <xsl:when test="contains(., 'CHOICE')">Choice</xsl:when>
    <xsl:otherwise><!-- Fail fast --></xsl:otherwise>
  </xsl:choose>
</xsl:variable>
<xsl:element name="{$outputElementName}">
  <xsl:copy-of select="@* | node()"/>
</xsl:element>
</xsl:template>