2016-04-25 84 views
0

我想变换分析下面的XML:XSLT:解析XML并重新构造XML?

<root> 
    <ParaList> 
    <Para name="p001" label="Continuous1" /> 
    <Para name="p002" label="Continuous5" /> 
    <Para name="p003" label="[Categorical3=1]" /> 
    <Para name="p004" label="[Categorical3=2]" /> 
    <Para name="p005" label="[Categorical3=3]" /> 
    </ParaList> 

    <ParaMatrix> 
    <PCell paraName="p001" coef="1.043"/> 
    <PCell paraName="p002" coef="0.841"/> 
    <PCell paraName="p003" coef="0.907"/> 
    <PCell paraName="p004" coef="-0.26"/> 
    <PCell paraName="p005" coef="5.333"/> 
    </ParaMatrix> 
</root> 

到一个新的XML:

<root> 
    <PMatrix> 
    <NumericCell paraName="Continuous1" coef="1.043"/> 
    <NumericCell paraName="Continuous5" coef="0.841"/> 
    <CategoricalCell value="1" paraName="Categorical3" coef="0.907"/> 
    <CategoricalCell value="2" paraName="Categorical3" coef="-0.26"/> 
    <CategoricalCell value="3" paraName="Categorical3" coef="5.333"/> 
    </PMatrix> 
</root> 

在原始XML中,有3个变量Continous1,Continuous5,Categorical3。连续1 &连续5是连续数值,分类3是3个可能值(1,2,3)的分类,它们被视为3个独立参数(p003,p004,p005)。每个参数都有一个系数。 可能有更多的变量,他们不遵循任何命名转向。你只能说有些是绝对的,因为label =“[categoricalVariableName = value]”。每个分类变量,如果它有M个可能的值,则生成M个参数。

我想解析原始的XML,得到系数(系数)直接映射到变量,而不是参数。如果它是一个连续变量,

<NumericCell paraName="Continuous1" coef="1.043"/> 

如果是分类变量,

<CategoricalCell value="1" paraName="Categorical3" coef="0.907"/> 
<CategoricalCell value="2" paraName="Categorical3" coef="-0.26"/> 
<CategoricalCell value="3" paraName="Categorical3" coef="5.333"/> 

我使用XSLT 1.0版本。非常感谢你的帮助。

+0

什么版本的XSLT?你有什么尝试? –

+0

问题不明确。输入中总会有5个'Para'元素? –

+0

我已经重写了我原来的问题。我正在使用XSLT版本1.0。我没有多少尝试,因为我不知道该怎么做。 Th – Fischlein

回答

0

您的问题留下了很多猜测的空间,您的示例输出甚至不是格式良好的,但让我们假设您想要的是:对于每个ParaList/Para Z,按如下方式生成PMatrix的子项:如果Z/@标签的形式为pN,则生成一个等于Z/@标签的@paraName,以及从相应PCell获取的coef。如果Z/@标签的形式为[pN = M],则生成一个值为= M,paraName = pN,coef如上所示。

这留下了很多未解决的情况,例如标签与这些模式中的任何一个都不匹配。但是,这会给这样的事情:

<xsl:key name="cell" match="ParaMatrix/PCell" use="@paraName"/> 

<xsl:template match="/"> 
    <root> 
    <PMatrix> 
    <xsl:apply-templates select="root/ParaList/Para"/> 
    </PMatrix> 
    </root> 
</xsl:template> 

<xsl:template match="Para[matches(@label, 'p[0-9]')"> 
    <NumericCell paraName="{@label}" coef="{key('cell', @name)/@coef}"/> 
</xsl:template> 

<xsl:template match="Para[matches(@label, '\[p[0-9]=[0-9]+\]')]"> 
    <CategoricalCell coef="{key('cell', @name)/@coef}"> 
    <xsl:analyze-string select="@label" regex="\[(p[0-9])=([0-9]+)\]"> 
    <xsl:matching-substring> 
    <xsl:attribute name="value" select="regex-group(2)"/> 
    <xsl:attribute name="paraName" select="regex-group(1)"/> 
    </xsl:matching-substring> 
    <xsl:non-matching-substring/> 
    </xsl:analyze-string> 
    </CategoricalCell> 
</xsl:template> 
+0

你好迈克尔,谢谢你的评论。我意识到我的问题在某些方面并不明确。所以我重写了我的问题。变量的名称(超出这些变量=>参数)不遵循任何命名约定。非常感谢您的专业知识。 – Fischlein

+0

然后,只需将我的示例中的正则表达式替换为更宽泛的表达式,例如p [0-9]变为[A-Za-z] [A-Za-z0-9] *。 –

+0

你好迈克尔,我试了你的代码,但是得到了语法错误,“未知函数匹配”,这是因为我使用的是xsl版本1.0吗?能否请你帮我解决1.0的问题?非常感谢你 – Fischlein