所以我现在已经在这方面工作了一段时间,我有点迷失了。所以在方向的底部,我有步骤,我已经编号我的步骤,但由于某种原因,我的xml中的文本没有被转移。
这是XML文件
<recipeml>
<recipe>
<head>
<title>Coq au Riesling</title>
</head>
<ingredients>
<ing>
<amt>
<qty>30</qty>
<unit system="metric">ml</unit>
</amt>
<item>garlic-infused olive oil</item>
</ing>
<ing>
<amt>
<qty>150</qty>
<unit system="metric" unit="g">gram(s)</unit>
</amt>
<item>bacon lardons</item>
</ing>
<ing>
<amt>
<qty>1</qty>
</amt>
<item>leek(s)</item>
<prep>(finely sliced)</prep>
</ing>
<ing>
<amt>
<qty>12</qty>
</amt>
<item>chicken thighs (boneless and skinned)</item>
</ing>
<ing>
<amt>
<qty>3</qty>
</amt>
<item>bay leaves</item>
</ing>
<ing>
<amt>
<qty>300</qty>
<unit system="metric">gram(s)</unit>
</amt>
<item>oyster mushroom(s) (torn into strips)</item>
</ing>
<ing>
<amt>
<qty>750</qty>
<unit system="metric">ml</unit>
</amt>
<item>Riesling</item>
</ing>
<ing>
<amt>
<qty> 1</qty>
<unit>splash of</unit>
</amt>
<item>double cream (optional)</item>
</ing>
<ing>
<amt>
<qty> 1</qty>
<unit>pinch of</unit>
</amt>
<item>salt</item>
</ing>
<ing>
<amt>
<qty>1</qty>
<unit>pinch of </unit>
</amt>
<item>Pepper</item>
</ing>
<ing>
<amt>
<qty>1</qty>
<unit>tablespoon(s)</unit>
</amt>
<item>dill (chopped to serve (2 tablespoons if needed)) </item>
</ing>
</ingredients>
<directions>
<step> Heat the oil in a casserole or large, wide pan and fry the lardons until crisp.</step>
<step> Add the sliced leek and soften it with the lardons for a minute or so.</step>
<step> Cut chicken thighs into 2 or 3 pieces each, tip them into the pan with the bay leaves, torn mushrooms and wine. </step>
<step>Season with salt and pepper to taste and bring to the boil, cover the pan and simmer gently for 30-40 minutes, stirring in the double cream for the last couple of minutes if you want. </step>
<note> Like all stews, this tastes its mellowest best if you let it get cold and then reheat the next day. But it's no hardship to eat straight off. Whichever, serve sprinkled with dill and together with some buttered noodles.</note>
</directions>
</recipe>
</recipeml>
这就是我在XSLT文件中的内容
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="ingredients">
<h1>Ingredients</h1>
<xsl:text> </xsl:text>
<xsl:for-each select="ing">
<p>
<xsl:choose>
<xsl:when test="prep">
<xsl:text> </xsl:text>
<xsl:value-of select="amt/qty"/>
<xsl:text> </xsl:text>
<xsl:value-of select="amt/unit"/>
<xsl:text> </xsl:text>
<xsl:value-of select="item"/>
<xsl:text> </xsl:text>
<span style="font-style:italic">
<xsl:value-of select="prep"/>
</span>
<xsl:text> </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
<xsl:value-of select="amt/qty"/>
<xsl:text> </xsl:text>
<xsl:value-of select="amt/unit"/>
<xsl:text> </xsl:text>
<xsl:value-of select="item"/>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</p>
</xsl:for-each>
</xsl:template>
<xsl:template match="directions">
<h2>Methods</h2>
<xsl:text> </xsl:text>
<xsl:for-each select="step">
<xsl:number format="1."/>
<xsl:text> </xsl:text>
</xsl:for-each>
<xsl:value-of select="step"/>
<xsl:text> </xsl:text>
<p>
<xsl:value-of select="note"/>
<br/>
</p>
</xsl:template>
</xsl:stylesheet>
现在发生的事情是,我希望我的方向标题为步骤编号,我确实让它们编号,但由于某种原因,xml中的文本不会转移。我搞砸了某个地方,但不完全确定在哪里:(
答案 0 :(得分:0)
由于目标格式似乎是HTML,我建议将这些step
转换为HTML有序列表:
<xsl:template match="directions">
<h2>Methods</h2>
<xsl:text> </xsl:text>
<ol>
<xsl:apply-templates select="step"/>
</ol>
<xsl:text> </xsl:text>
<p>
<xsl:value-of select="note"/>
<br/>
</p>
</xsl:template>
<xsl:template match="step">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
这样,呈现结果的任何HTML用户代理都会对step
已转换为的列表项进行编号。
答案 1 :(得分:0)
这个逻辑
<xsl:for-each select="step">
<xsl:number format="1."/>
<xsl:text> </xsl:text>
</xsl:for-each>
<xsl:value-of select="step"/>
在XSLT 1.0和2.0中有不同的效果,你还没有说明你使用的是什么。但在这两种情况下,这都是错误的。您正在为每个步骤输出一个数字,然后在最后考虑输出步骤的值。在XSLT 1.0中,xsl:value-of将输出第一步的内容,在2.0中它将输出所有步骤的值,以空格分隔。你真正想要的是:
<xsl:for-each select="step">
<xsl:number format="1."/>
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
</xsl:for-each>