问题描述
说我有一个XML文件,例如:
say I have an XML file like:
<library>
<books>
<![CDATA[<genre><name>Sci-fi</name><count>2</count></genre>]]>
<book>
<name>
Some Book
</name>
<author>
Some author
</author>
<book>
<book>
<name>
Another Book
</name>
<author>
Another author
</author>
<book>
<books>
</library>
我想在xslt转换器中读取CDATA元素名称",并将其值放置在标签的值中.我该怎么做呢?AFAIK,我们不能在CDATA的内容上使用xpath.是否有一些破解/解决方法?我想在XSLT中严格执行此操作.
I want to read the CDATA element 'name' in an xslt transformer and place its value somewhere in the vaue of a tag. How do I do this? AFAIK, we cannot use xpath on the contents of CDATA. Is there some hack/workaround for this? I want to do this strictly in an XSLT.
推荐答案
由于CDATA块是文本节点(的一部分),因此您可以提取两个标签"之间的文本,例如像这样:
Since CDATA blocks are (part of) text nodes, you can extract the text between the two "tags", e.g. like this:
<xsl:template match="text()">
<xsl:value-of select="substring-before(substring-after(., '<name>'), '</name>')"/>
</xsl:template>
这只是一个简单的想法.如果CDATA中有多个名称元素",则只需递归地多次应用上述表达式即可.
This is just a quick idea. If you have more than one name "element" inside the CDATA, just recursively apply the above expression multiple times.
这篇关于如何解析XSLT中CDATA元素内的XML DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!