我有一个源xml:
<root>
<DataDictionary>
<DataField name="CLASS">
<Value value="0" property="valid"/>
<Value value="1" property="valid"/>
<Value value="2" property="valid"/>
</DataField>
<DataField name="COUNTRY_CODE">
<Value value="1" property="valid"/>
<Value value="2" property="valid"/>
<Value value="3" property="valid"/>
</DataField>
<DataField name="MARITAL_STATUS">
<Value value="1" property="valid"/>
<Value value="2" property="valid"/>
<Value value="3" property="valid"/>
</DataField>
</DataDictionary>
<RegressionModel targetVariable="CLASS">
<ParamMatrix>
<PCell name="COUNTRY_CODE" targetCategory="0" coefficient="12"/>
<PCell name="MARITAL_STATUS" targetCategory="0" coefficient="34"/>
</ParamMatrix>
</RegressionModel>
<root>
转型后,我想:
<root>
<DataDictionary>
<DataField name="CLASS">
<Value value="0" property="valid"/>
<Value value="1" property="valid"/>
<Value value="2" property="valid"/>
</DataField>
<DataField name="COUNTRY_CODE">
<Value value="1" property="valid"/>
<Value value="2" property="valid"/>
<Value value="3" property="valid"/>
</DataField>
<DataField name="MARITAL_STATUS">
<Value value="1" property="valid"/>
<Value value="2" property="valid"/>
<Value value="3" property="valid"/>
</DataField>
</DataDictionary>
</Schema>
<RegressionModel targetVariable="CLASS">
<ParamMatrix>
<PCell name="COUNTRY_CODE" targetCategory="0" coefficient="12"/>
<PCell name="MARITAL_STATUS" targetCategory="0" coefficient="34"/>
</ParamMatrix>
</RegressionModel>
<!--other transformations -->
<RegressionTable targetVariable="CLASS" targetCategory="1" />
<RegressionTable targetVariable="CLASS" targetCategory="2" />
<root>
我还没有列出其他变化。我遇到的问题是2&#39; RegressionTable&#39;文件末尾的元素。用于构建转换的逻辑是:
元素看起来像:
<RegressionTable
targetVariable=DataDictionary/DataField/@name
targetCategory=DataDictionary/DataField/Value/@value />
我真的很感谢你的帮助。感谢。
答案 0 :(得分:0)
如果我按照您的说明逐字(AFAICT):
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="dataField" match="DataField" use="@name" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="RegressionModel" mode="reg-tab"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RegressionModel" mode="reg-tab">
<xsl:variable name="targetVariable" select="@targetVariable" />
<xsl:variable name="targetCategory" select="ParamMatrix/PCell[1]/@targetCategory" />
<xsl:for-each select="key('dataField', $targetVariable)/Value[@value != $targetCategory]">
<RegressionTable targetVariable="{$targetVariable}" targetCategory="{@value}" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我得到以下结果:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<DataDictionary>
<DataField name="CLASS">
<Value value="0" property="valid"/>
<Value value="1" property="valid"/>
<Value value="2" property="valid"/>
</DataField>
<DataField name="COUNTRY_CODE">
<Value value="1" property="valid"/>
<Value value="2" property="valid"/>
<Value value="3" property="valid"/>
</DataField>
<DataField name="MARITAL_STATUS">
<Value value="1" property="valid"/>
<Value value="2" property="valid"/>
<Value value="3" property="valid"/>
</DataField>
</DataDictionary>
<RegressionModel targetVariable="CLASS">
<ParamMatrix>
<PCell name="COUNTRY_CODE" targetCategory="0" coefficient="12"/>
<PCell name="MARITAL_STATUS" targetCategory="0" coefficient="34"/>
</ParamMatrix>
</RegressionModel>
<RegressionTable targetVariable="CLASS" targetCategory="1"/>
<RegressionTable targetVariable="CLASS" targetCategory="2"/>
</root>
这不完全是您显示的结果。