我有以下带有cdata标记的XML,我想从中提取文本?
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<cus:TestData xmlns:cus="http://test.namespace.com/data">
<![CDATA[testValue]]></cus:TestData >
如何在XSLT中实现这一目标?
我正在尝试以下
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
"<xsl:value-of select="/*/Name"/>"
</xsl:template>
</xsl:stylesheet>
但它似乎没有起作用
此外,XML也没有相同的前缀或命名空间,它会更改
答案 0 :(得分:0)
这对CData来说并不是一个问题。您的XSLT当前正在根元素下查找名为Name
的元素,该元素在XML中不存在。如果您的XML源是您实际使用的源,那么您可以这样做......
<xsl:value-of select="/*"/>
但假设您的XML看起来像这样......
<cus:TestData xmlns:cus="http://test.namespace.com/data">
<cus:Name><![CDATA[testValue]]></cus:Name>
</cus:TestData>
然后,您需要考虑XSLT中的命名空间,因为Name
位于XML中的命名空间中,但您的XSLT当前正在寻找无命名空间中的Name
元素。 / p>
这样的事情可以做到:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="http://test.namespace.com/data">
<xsl:output method="text"/>
<xsl:template match="/">
"<xsl:value-of select="/*/c:Name"/>"
</xsl:template>
</xsl:stylesheet>
请注意,前缀不需要匹配,但名称空间URI可以匹配。
如果命名空间URI实际上可能有所不同,你可以做这样的事情......
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
"<xsl:value-of select="/*/*[local-name() = 'Name']"/>"
</xsl:template>
</xsl:stylesheet>