xsl - 在父级中混合文本的标记位置 [英] xsl - tag position with text mixed in parent
问题描述
在 XSL 1.0 中,我试图区分以下 2 种情况,它们都出现在我需要处理的输入 XML 中,并且每种情况都以不同的方式处理.
In XSL 1.0, I am trying to distinguish between the following 2 scenarios which both occur within the input XML I need to process and each to be handled differently.
场景一
<tag1><tag2/> some text</tag1>
场景 2
<tag1>some text <tag2/></tag1>
我有一个在 <tag2/>
级别匹配的模板,在此我希望场景 1 在场景中忽略 <tag2/>
2 我想插入一个
代替
.
I have a template which matches at the <tag2/>
level, within this I want scenario 1 to ignore <tag2/>
, in scenario 2 I want to insert a <br/>
in place of <tag2/>
.
我已经在这里和谷歌上搜索过,但似乎无法弄清楚如何根据
在
中的位置来区分代码>.
I have searched on here and on google but can't seem to figure out how to distinguish based on the position of <tag2/>
within <tag1>
.
我研究了 preceding-sibling 和 generate-id 并尝试使用类似的东西:-
I have looked into preceding-sibling and generate-id and tried to use something like:-
not(
generate-id(
preceding-sibling::node()[1]
)
= generate-id(
preceding-sibling::text()[1]
)
)
和
position()
of <tag2/>
在这里似乎也没有帮助,因为两者似乎都在节点级别运行??
of <tag2/>
doesn't seem to help here either as both appear to be operating at the node level??
欢迎有任何想法吗?
谢谢罗杰
推荐答案
以下样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="tag2[preceding-sibling::text()]">
<br/>
</xsl:template>
</xsl:stylesheet>
在这个输入上:
<items>
<tag1><tag2/>some text</tag1>
<tag1>some text <tag2/></tag1>
</items>
产生:
some text
some text <br/>
更复杂的解决方案需要有关所需输出的附加信息.
A more complex solution would require additional information about the desired output.
这篇关于xsl - 在父级中混合文本的标记位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!