I am new to XSLT.
我是XSLT的新手。
I have an XML-
我有一个XML-
<root>
<fiberList>
<fiber>
<A>abcd</A>
<B>bcde</B>
<C>cdef</C>
</fiber>
</fiberList>
<generatorList>
<generator>
<D>defg</D>
<E>efgh</E>
</generator>
</root>
I want to transform this XML into another XML through XSL. The resulting XML should be something like this-
我想通过XSL将这个XML转换为另一个XML。生成的XML应该是这样的 -
<parent>
<child>
<M>abcd</M>
<N>bcde</N>
<O>efgh</O>
</child>
</parent>
But I am getting the following XML after transformation-
但我在转换后得到以下XML -
<parent>
<child>
<M>abcd</M>
<N>bcde</N>
<O/>
</child>
</parent>
My XSL is the following-
我的XSL如下 -
<xsl:template match="/">
<xsl:element name="parent">
<xsl:apply-templates select="root/fiberList/fiber"/>
</xsl:element>
</xsl:template>
<xsl:template match="root/fiberList/fiber">
<xsl:element name="child">
<xsl:element name="M">
<xsl:value-of select="A"/>
</element>
<xsl:element name="N">
<xsl:value-of select="B"/>
</element>
<xsl:element name="O">
<xsl:value-of select="root/generatorList/generator/E"/>
</element>
</element>
</template>
</stylesheet>
Please help.
2 个解决方案
#1
0
i haven't tried it .. but i think a leading '/' is missing here :
我还没试过..但我认为这里缺少一个领先的'/':
<xsl:value-of select="root/generatorList/generator/E"/>
try this please :
请试试这个:
<xsl:value-of select="/root/generatorList/generator/E"/>
otherwise the xslt engine is trying to find a relative path "root/generatorList/generator/E" starting at the matched element (/root/fiberList/fiber) of the template but there is no /root/fiberList/fiber/root/generatorList/generator/E.
否则xslt引擎试图从模板的匹配元素(/ root / fiberList / fiber)开始寻找相对路径“root / generatorList / generator / E”,但没有/ root / fiberList / fiber / root / generatorList /发电机/ E。
#2
0
As an alternate, you could use this stylesheet too:
作为替代方案,您也可以使用此样式表:
XSLT-1.0:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/root">
<parent>
<child>
<xsl:apply-templates select="fiberList/fiber/A | fiberList/fiber/B | generatorList/generator/E"/>
</child>
</parent>
</xsl:template>
<xsl:template match="A">
<M>
<xsl:value-of select="."/>
</M>
</xsl:template>
<xsl:template match="B">
<N>
<xsl:value-of select="."/>
</N>
</xsl:template>
<xsl:template match="E">
<O>
<xsl:value-of select="."/>
</O>
</xsl:template>
</xsl:stylesheet>
EDIT:
In XSLT-2.0 you can simplify the xsl:apply-templates
instruction to the following:
在XSLT-2.0中,您可以将xsl:apply-templates指令简化为以下内容:
<xsl:apply-templates select="fiberList/fiber/(A | B) | generatorList/generator/E"/>
#1
0
i haven't tried it .. but i think a leading '/' is missing here :
我还没试过..但我认为这里缺少一个领先的'/':
<xsl:value-of select="root/generatorList/generator/E"/>
try this please :
请试试这个:
<xsl:value-of select="/root/generatorList/generator/E"/>
otherwise the xslt engine is trying to find a relative path "root/generatorList/generator/E" starting at the matched element (/root/fiberList/fiber) of the template but there is no /root/fiberList/fiber/root/generatorList/generator/E.
否则xslt引擎试图从模板的匹配元素(/ root / fiberList / fiber)开始寻找相对路径“root / generatorList / generator / E”,但没有/ root / fiberList / fiber / root / generatorList /发电机/ E。
#2
0
As an alternate, you could use this stylesheet too:
作为替代方案,您也可以使用此样式表:
XSLT-1.0:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/root">
<parent>
<child>
<xsl:apply-templates select="fiberList/fiber/A | fiberList/fiber/B | generatorList/generator/E"/>
</child>
</parent>
</xsl:template>
<xsl:template match="A">
<M>
<xsl:value-of select="."/>
</M>
</xsl:template>
<xsl:template match="B">
<N>
<xsl:value-of select="."/>
</N>
</xsl:template>
<xsl:template match="E">
<O>
<xsl:value-of select="."/>
</O>
</xsl:template>
</xsl:stylesheet>
EDIT:
In XSLT-2.0 you can simplify the xsl:apply-templates
instruction to the following:
在XSLT-2.0中,您可以将xsl:apply-templates指令简化为以下内容:
<xsl:apply-templates select="fiberList/fiber/(A | B) | generatorList/generator/E"/>