XML动态标记名称

时间:2016-08-08 05:42:44

标签: xml xslt tags

我是xslt的新手,我需要动态更改标记名称。如源XML所示,G_1将包含MapIDSTAGE_COLUMN_NAME,我们需要从此处获取VO_ATTRIBUTE_NAME,同样应在G_2中替换为标记名称

源XML -

<?xml version="1.0" encoding="UTF-8"?>
<DATA_DS>
   <G_1>
      <MapID>100</MapID>
      <STAGE_COLUMN_NAME>XXDATE001</STAGE_COLUMN_NAME>
      <VO_ATTRIBUTE_NAME>StartDate</VO_ATTRIBUTE_NAME>
   </G_1>
   <G_1>
      <MapID>100</MapID>
      <STAGE_COLUMN_NAME>XXDATE002</STAGE_COLUMN_NAME>
      <VO_ATTRIBUTE_NAME>EndDate</VO_ATTRIBUTE_NAME>
   </G_1>
   <G_1>
      <MapID>100</MapID>
      <STAGE_COLUMN_NAME>XXVAL001</STAGE_COLUMN_NAME>
      <VO_ATTRIBUTE_NAME>OrgID</VO_ATTRIBUTE_NAME>
   </G_1>
   <G_1>
      <MapID>100</MapID>
      <STAGE_COLUMN_NAME>XXVAL002</STAGE_COLUMN_NAME>
      <VO_ATTRIBUTE_NAME>DepName</VO_ATTRIBUTE_NAME>
   </G_1>
   <G_1>
      <MapID>200</MapID>
      <STAGE_COLUMN_NAME>XXDATE001</STAGE_COLUMN_NAME>
      <VO_ATTRIBUTE_NAME>StartDate Two</VO_ATTRIBUTE_NAME>
   </G_1>
   <G_1>
      <MapID>200</MapID>
      <STAGE_COLUMN_NAME>XXDATE002</STAGE_COLUMN_NAME>
      <VO_ATTRIBUTE_NAME>EndDate Two</VO_ATTRIBUTE_NAME>
   </G_1>
   <G_1>
      <MapID>200</MapID>
      <STAGE_COLUMN_NAME>XXVAL001</STAGE_COLUMN_NAME>
      <VO_ATTRIBUTE_NAME>DepID</VO_ATTRIBUTE_NAME>
   </G_1>
   <G_1>
      <MapID>200</MapID>
      <STAGE_COLUMN_NAME>XXVAL002</STAGE_COLUMN_NAME>
      <VO_ATTRIBUTE_NAME>Division Name</VO_ATTRIBUTE_NAME>
   </G_1>
   <G_2>
      <PHYSICAL_LINE_ID>123456789</PHYSICAL_LINE_ID>
      <MapID>100</MapID>
      <XXVAL002>Emergency Dept.-East - 400750</XXVAL002>
      <XXDATE001>1951-01-01T00:00:00.000+00:00</XXDATE001>
      <XXDATE002>4712-12-31T00:00:00.000+00:00</XXDATE002>
   </G_2>
   <G_2>
      <PHYSICAL_LINE_ID>123456789</PHYSICAL_LINE_ID>
      <MapID>200</MapID>
      <XXVAL002>Emergency Division - 4353</XXVAL002>
      <XXDATE001>1951-01-01T00:00:00.000+00:00</XXDATE001>
      <XXDATE002>4712-12-31T00:00:00.000+00:00</XXDATE002>
   </G_2>
</DATA_DS>

目标XML

<?xml version="1.0" encoding="UTF-8"?>
<DATA_DS>
   <G_2>
      <PHYSICAL_LINE_ID>100000007956555</PHYSICAL_LINE_ID>
      <DepName>Emergency Dept.-East - 400750</DepName>
      <StartDate>1951-01-01T00:00:00.000+00:00</StartDate>
      <EndDate>4712-12-31T00:00:00.000+00:00</EndDate>
   </G_2>
      <G_2>
      <PHYSICAL_LINE_ID>100000007956555</PHYSICAL_LINE_ID>
      <Division Name>Emergency Division - 4353</Division Name>
      <StartDate Two>1951-01-01T00:00:00.000+00:00</StartDate Two>
      <EndDate Two>4712-12-31T00:00:00.000+00:00</EndDate Two>
   </G_2>
</DATA_DS>

请尽可能帮助我使用xslt代码或在此方向提示。

1 个答案:

答案 0 :(得分:1)

有趣的小问题。这是一个XSLT 1.0解决方案:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:key name="fields" match="//G_1" use="concat(MapID/text(), STAGE_COLUMN_NAME/text())"/>
    <xsl:template match="/">
        <DATA_DS>
            <xsl:apply-templates select="//G_2"/>
        </DATA_DS>
    </xsl:template>
    <xsl:template match="G_2">
        <G_2>
            <xsl:apply-templates select="*"/>
        </G_2>
    </xsl:template>
    <xsl:template match="MapID"/>
    <xsl:template match="*">
        <xsl:variable name="id" select="concat(../MapID/text(),name())"/>
        <xsl:variable name="x" select="key('fields',$id)/VO_ATTRIBUTE_NAME/text()"/>
        <xsl:choose>
            <xsl:when test="$x">
                <xsl:element name="{translate($x,' ','-')}">
                    <xsl:value-of select="text()"/>
                </xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

它使用一个键(map)来查找标记名称,并通过不具有映射的未触摸标记。请注意,元素名称不能包含空格,因此我将空格转换为连字符,这是合法的。

您的样本输入的输出是:

<?xml version="1.0" encoding="utf-8"?>
<DATA_DS>
   <G_2>
      <PHYSICAL_LINE_ID>123456789</PHYSICAL_LINE_ID>
      <DepName>Emergency Dept.-East - 400750</DepName>
      <StartDate>1951-01-01T00:00:00.000+00:00</StartDate>
      <EndDate>4712-12-31T00:00:00.000+00:00</EndDate>
   </G_2>
   <G_2>
      <PHYSICAL_LINE_ID>123456789</PHYSICAL_LINE_ID>
      <Division-Name>Emergency Division - 4353</Division-Name>
      <StartDate-Two>1951-01-01T00:00:00.000+00:00</StartDate-Two>
      <EndDate-Two>4712-12-31T00:00:00.000+00:00</EndDate-Two>
   </G_2>
</DATA_DS>