为了结合每道菜的所有条件和测量值,我将如何转换以下内容?我正在努力组合重复的子元素。
<?xml version="1.0" encoding="UTF-8"?>
<Test>
<Experiment id='1'>
<Dish1>
<Conditions pressure='x' temp='y'/>
<Measurement timeStamp='8am' reading='y'/>
</Dish1>
<Dish2>
<Conditions pressure='x' temp='y'/>
<Measurement timeStamp='8am' reading='y'/>
</Dish2>
<Dish1>
<Conditions pressure='x' temp='y'/>
<Measurement timeStamp='2pm' reading='y'/>
</Dish1>
<Dish2>
<Conditions pressure='x' temp='y'/>
<Measurement timeStamp='2pm' reading='y'/>
</Dish2>
</Experiment>
<Experiment id='2'>
<Dish1>
<Conditions pressure='x' temp='y'/>
<Measurement timeStamp='9am' reading='y'/>
</Dish1>
</Experiment>
<Experiment id='2'>
<Dish1>
...
</Test>
期望的结果:
<?xml version="1.0" encoding="UTF-8"?>
<Test>
<Experiment id='1'>
<Dish1>
<Observation pressure='x' temp='y' timeStamp='8am' reading='y'/>
<Observation pressure='x' temp='y' timeStamp='2pm' reading='y'/>
</Dish1>
<Dish2>
<Observation pressure='x' temp='y' timeStamp='8am' reading='y'/>
<Observation pressure='x' temp='y' timeStamp='2pm' reading='y'/>
</Dish2>
</Experiment>
<Experiment id='2'>
<Dish1>
<Observation pressure='x' temp='y' timeStamp='9am' reading='y'/>
...
谢谢,麻烦您了!
这是我到目前为止所尝试的:
<?xml version="1.0" encoding="UTF-8"?>
<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:template match="Experiment">
<xsl:copy>
<xsl:for-each select="Dish1">
<xsl:element name="Observation">
<xsl:attribute name="pressure"><xsl:value-of select="Conditions/@pressure"/></xsl:attribute>
<xsl:attribute name="temp"><xsl:value-of select="Conditions/@temp"/></xsl:attribute>
<xsl:attribute name="TimeStamp"><xsl:value-of select="Measurement/@TimeStamp"/></xsl:attribute>
<xsl:attribute name="reading"><xsl:value-of select="Measurement/@reading"/></xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<!-- copy everthing not covered above-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在写下我失败的尝试时,我想出了上述转变。它似乎正在工作,但我需要验证输出。任何建议/改进将不胜感激。谢谢你。
...我的转换仅适用于第一个实验。当我添加:
<xsl:template match="Experiment">
<xsl:copy>
<xsl:for-each select="Dish2">
<xsl:element name="Observation">
<xsl:attribute name="pressure"><xsl:value-of select="Conditions/@pressure"/></xsl:attribute>
<xsl:attribute name="temp"><xsl:value-of select="Conditions/@temp"/></xsl:attribute>
<xsl:attribute name="TimeStamp"><xsl:value-of select="Measurement/@TimeStamp"/></xsl:attribute>
<xsl:attribute name="reading"><xsl:value-of select="Measurement/@reading"/></xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>
...它找不到任何 Dish2 元素。
感谢 Ian Roberts,让我写下我失败的尝试引发了一个想法,这导致了下面的工作解决方案。
<?xml version="1.0" encoding="UTF-8"?>
<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:template match="Experiment">
<xsl:copy>
<xsl:for-each select="Dish1">
<xsl:element name="Observation">
<xsl:attribute name="pressure"><xsl:value-of select="Conditions/@pressure"/></xsl:attribute>
<xsl:attribute name="temp"><xsl:value-of select="Conditions/@temp"/></xsl:attribute>
<xsl:attribute name="TimeStamp"><xsl:value-of select="Measurement/@TimeStamp"/></xsl:attribute>
<xsl:attribute name="reading"><xsl:value-of select="Measurement/@reading"/></xsl:attribute>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="Dish2">
<xsl:element name="Observation">
<xsl:attribute name="pressure"><xsl:value-of select="Conditions/@pressure"/></xsl:attribute>
<xsl:attribute name="temp"><xsl:value-of select="Conditions/@temp"/></xsl:attribute>
<xsl:attribute name="TimeStamp"><xsl:value-of select="Measurement/@TimeStamp"/></xsl:attribute>
<xsl:attribute name="reading"><xsl:value-of select="Measurement/@reading"/></xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<!-- copy everthing not covered above-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>