0

我有一个要求,我必须使用 xslt 进行转换,已知标签要被替换,但是对于任何未识别的标签都应该放在一个属性包中(比如在 c# 中)类型的模式。例如。我想转换以下 XML:

<Envelope>
    <body>
        <MyOperation_reply_data>
            <operation_name>1211</operation_name>
            <operation_short_desc>SCAMA</operation_short_desc>
            <operation_long_desc>C4 SCAM PIECE PART ASSEMBLY
            </operation_long_desc>
            <primary_units>UNITS</primary_units>
            <firstAttribute>123</firstAttribute>
            <sampleone>554</sampleone>
            <area>newarea</area>
            <group>samplegroup</group>
            <whatever>uuu</whatever>
        </MyOperation_reply_data>
    </body>
</Envelope>

到这种格式:

<Envelope>
<Body>
    <OperationInfo OperationName="MyOperation">
        <OperationData>
            <Operation>MyOperation</Operation>
            <ShortDescription>MyDescription</ShortDescription>
            <LongDescription>MyLongDescription</LongDescription>
            <PrimaryUnits>UNITS</PrimaryUnits>
            <Attributes>
                <Attribute Name="firstAttribute" Value="123" />
                <Attribute Name="sampleone" Value="554" />
                <Attribute Name="area" Value="newarea" />
                <Attribute Name="group" Value="samplegroup" />
                <Attribute Name="whatever" Value="uuu" />
            </Attributes>
        </OperationData>
    </OperationInfo>
</Body>

如您所见,只有操作、操作长/短描述和主要单位是可识别的标签。我还想把它放在上面的属性列表中。“其他”标签列表可能会增长 - 所以我们的想法是将所有其他标签放入这种属性包类型的模式中。

我面临的挑战是我无法为每个或识别“其他”标签。我尝试了 selected not(self:: with or 语句,但没有太大帮助。

4

1 回答 1

1

在 XSLT 1.0 中,您可以使用xsl:key创建元素的“白名单”。

XML 输入

<Envelope>
    <body>
        <MyOperation_reply_data>
            <operation_name>1211</operation_name>
            <operation_short_desc>SCAMA</operation_short_desc>
            <operation_long_desc>C4 SCAM PIECE PART ASSEMBLY</operation_long_desc>
            <primary_units>UNITS</primary_units>
            <firstAttribute>123</firstAttribute>
            <sampleone>554</sampleone>
            <area>newarea</area>
            <group>samplegroup</group>
            <whatever>uuu</whatever>
        </MyOperation_reply_data>
    </body>
</Envelope>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="whiteList" match="*[self::primary_units or 
                                       self::operation_name or
                                       self::operation_short_desc or
                                       self::operation_long_desc]" use="name()"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="body">
        <Body>
            <xsl:apply-templates select="@*|node()"/>
        </Body>
    </xsl:template>

    <xsl:template match="MyOperation_reply_data">
        <OperationInfo OperationName="{operation_name}">
            <OperationData>
                <xsl:apply-templates select="*[key('whiteList',name())]"/>
                <Attributes>
                    <xsl:apply-templates select="*[not(key('whiteList',name()))]"/>                 
                </Attributes>
            </OperationData>
        </OperationInfo>
    </xsl:template>

    <xsl:template match="operation_name">
        <Operation><xsl:apply-templates/></Operation>
    </xsl:template>

    <xsl:template match="operation_short_desc">
        <ShortDescription><xsl:apply-templates/></ShortDescription>
    </xsl:template>

    <xsl:template match="operation_long_desc">
        <LongDescription><xsl:apply-templates/></LongDescription>
    </xsl:template>

    <xsl:template match="primary_units">
        <PrimaryUnits><xsl:apply-templates/></PrimaryUnits>
    </xsl:template>

    <xsl:template match="MyOperation_reply_data/*[not(key('whiteList',name()))]">
        <Attribute name="{name()}" value="{.}"/>
    </xsl:template>

</xsl:stylesheet>

XML 输出

<Envelope>
   <Body>
      <OperationInfo OperationName="1211">
         <OperationData>
            <Operation>1211</Operation>
            <ShortDescription>SCAMA</ShortDescription>
            <LongDescription>C4 SCAM PIECE PART ASSEMBLY</LongDescription>
            <PrimaryUnits>UNITS</PrimaryUnits>
            <Attributes>
               <Attribute name="firstAttribute" value="123"/>
               <Attribute name="sampleone" value="554"/>
               <Attribute name="area" value="newarea"/>
               <Attribute name="group" value="samplegroup"/>
               <Attribute name="whatever" value="uuu"/>
            </Attributes>
         </OperationData>
      </OperationInfo>
   </Body>
</Envelope>
于 2013-01-30T01:18:13.430 回答