我有一个要求,我必须使用 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>
正如您只能看到的操作,操作长/短描述和主要单位是可识别的标签。 我想像上面一样将其放在属性列表中的任何其他内容。"其他"标签列表可能会增长 - 所以这个想法是将所有其他标签放入这种属性包类型的模式中。
我面临的挑战是我无法为每个标签做或识别"其他"标签。我尝试选择不(self:: with or statement,但没有多大帮助。
在 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>