我正在研究 XSLT。我无法找到 xslt。我努力了。
来源:
<?xml version="1.0" encoding="ISO-8859-1"?>
<body>
<selectedComp>bodyParagraphText</selectedComp>
<value>
<p xmlns="http://www.w3.org/1999/xhtml">abd</p>
<p xmlns="http://www.w3.org/1999/xhtml"> </p>
<p xmlns="http://www.w3.org/1999/xhtml">afh</p>
<p xmlns="http://www.w3.org/1999/xhtml"> </p>
<p xmlns="http://www.w3.org/1999/xhtml">AAA</p>
<p xmlns="http://www.w3.org/1999/xhtml"> </p>
<p xmlns="http://www.w3.org/1999/xhtml">ZZZ</p>
</value>
</body>
XSLT 编写:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:widget="aaa">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="body/value/p">
<xsl:element name= "widget:bodyParagraphText">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
但我什么也没得到。我最后得到的是空 xml。
但需要输出:
<widget:bodyParagraphText>
<text>abd</text>
</widget:bodyParagraphText>
<widget:bodyParagraphText>
<text> </text>
</widget:bodyParagraphText>
<widget:bodyParagraphText>
<text>afh</text>
</widget:bodyParagraphText>
<widget:bodyParagraphText>
<text> </text>
</widget:bodyParagraphText>
<widget:bodyParagraphText>
<text>AAA</text>
</widget:bodyParagraphText>
<widget:bodyParagraphText>
<text> </text>
</widget:bodyParagraphText>
任何人都可以建议如何做到这一点吗?
谢谢。
最佳答案
p
元素的命名空间为 http://www.w3.org/1999/xhtml
,您需要将其包含在 XPath 表达式中:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:widget="aaa"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="body/value/xhtml:p">
<widget:bodyParagraphText>
<text>
<xsl:value-of select="."/>
</text>
</widget:bodyParagraphText>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
请注意根标记上添加的 xmlns:xhtml
属性,以及 XPath 表达式中的 xhtml:
前缀。
关于xslt - 无法识别 XSLT 中的 <p> 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10275427/