使用XPATH(和PHP)从样式属性中选择背景url
我只想从这个背景图像样式属性中选择url,使用XPATH可以吗使用XPATH(和PHP)从样式属性中选择背景url,php,dom,xpath,attributes,Php,Dom,Xpath,Attributes,我只想从这个背景图像样式属性中选择url,使用XPATH可以吗 <a href="http://www.test.com" style="background-image: url('http://www.test.com/hello.jpg');">test</a> 使用: substring-before(substring-after(@style, "'"), "'" ) <xsl:st
<a href="http://www.test.com" style="background-image: url('http://www.test.com/hello.jpg');">test</a>
使用:
substring-before(substring-after(@style, "'"),
"'"
)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
"<xsl:value-of select=
"substring-before(substring-after(@style, "'"),
"'"
)
"/>"
</xsl:template>
</xsl:stylesheet>
<a href="http://www.test.com" style=
"background-image: url('http://www.test.com/hello.jpg');">test</a>
"http://www.test.com/hello.jpg"
基于XSLT的验证:
substring-before(substring-after(@style, "'"),
"'"
)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
"<xsl:value-of select=
"substring-before(substring-after(@style, "'"),
"'"
)
"/>"
</xsl:template>
</xsl:stylesheet>
<a href="http://www.test.com" style=
"background-image: url('http://www.test.com/hello.jpg');">test</a>
"http://www.test.com/hello.jpg"
和?它有用吗?它会给你不想要的结果吗?它会给出style属性的完整内容,我只想选择url的内容(“”)style属性中的内容不再是DOM的一部分,因此它不是XPath的工作。你会找到一个CSS解析器来做这件事。。。我不知道有一个像XPath或DOMDocument那样常见的解决方案,也许这个特殊的例子最容易用正则表达式完成。好问题,+1。请参阅我的答案,以获得一个完整、简短、简单的一行XPath表达式,该表达式可以精确地生成所需的URL。纯代码答案对社区没有用处。请看