我想要處理一些節點並再次處理這些節點的子集,特別是那些'n'-ancestors具有直接子節點'x'的節點。模式輸出中的XSLT模板包含應該在其他模式下匹配的元素
的XML,簡化儘可能:
<root>
<s>
</s>
<s>
<n>
<w a="Hello"/>
<n>
<x/>
<w a="he said"/>
</n>
</n>
<n>
<w a="how are you"/>
</n>
<n>
</n>
<n>
<w a="she answered"/>
<n>
<n>
<x/>
<w a="friendly"/>
</n>
</n>
</n>
</s>
</root>
正如我必須處理一些W¯¯節點兩次,我決定使用模式。我有兩種模式(可讀性命名爲'fromone','fromtwo')與'a-attribute'相匹配,並且還嘗試了僅使用一種模式的各種組合,添加一個沒有模式的空模板,但結果保持不變。
的XSLT的相關部分,也簡化了,原來的模板大多匹配條件(除了模式的那些):
<xsl:template match="root/s">
<output>
<one>
<xsl:apply-templates select="descendant::w"/>
</one>
<x>
<xsl:apply-templates select="n"/>
</x>
</output>
</xsl:template>
<xsl:template match="n[x]">
<xsl:apply-templates select="descendant::w/@a" mode="fromtwo"/>
</xsl:template>
<xsl:template match="w">
<xsl:apply-templates select="@a" mode="fromone"/>
</xsl:template>
<xsl:template match="@a" mode="fromtwo">
<fromtwo>
<xsl:value-of select="."/>
</fromtwo>
</xsl:template>
<xsl:template match="@a" mode="fromone">
<fromone>
<xsl:value-of select="."/>
</fromone>
</xsl:template>
我希望輸出是這樣的:
<root>
<output>
<one>
<fromone>Hello</fromone>
<fromone>he said</fromone>
<fromone>how are you</fromone>
<fromone>she answered</fromone>
<fromone>friendly</fromone>
</one>
<x>
<fromtwo>he said</fromtwo>
<fromtwo>friendly</fromtwo>
</x>
</output>
</root>
但在實際的結果是,節點x包含我預計將只匹配來自其他模板呼叫到來元素,「fromone」:
<x>
<fromone>Hello</fromone>
<fromtwo>he said</fromtwo>
<fromone>how are you</fromone>
<fromone>she answered</fromone>
<fromtwo>friendly</fromtwo>
</x>
由於這是我嘗試使用的所有XSLT處理器的結果,因此我假設我的模板結構中存在錯誤。即使在閱讀其他文章的模式問題之後,我也沒有更接近解決方案或在XSLT中發現我的錯誤。
我犯了什麼錯誤?
爲什麼結果有兩個模板的混合輸出,而不是隻顯示'fromtwo'的輸出?
所以我也可以通過創建一個空的w-匹配模板來解決這個問題? –
是的,如果你從一開始就匹配模式。但選擇性應用既簡單又高效。 –