我试图弄清楚XSLT如何处理名称空间前缀并具有以下示例: XML: ?xml version="1.0" encoding="UTF-8"?feed xmlns="http://www.w3.org/2005/Atom" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:zno="http://feed.zinio.com/ato
XML:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:zno="http://feed.zinio.com/atom"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2005/Atom
http://www.w3.org/1999/xhtml
http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd
http://feed.zinio.com/atom" >
<entry>
<author>
<name>By Sheila F. Buckmaster</name>
</author>
<category xml:lang="en" term="TRAVEL"/>
<content>
<h2 class="hl2">One of the world’s most entrancing cities becomes even more captivating when costumed revelers fill its tiny streets and grand piazzas during Carnevale. It is here that a star of the silent screen comes alive, antics and all</h2>
<div class="byline">By Sheila F. Buckmaster</div>
</content>
</entry>
</feed>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"
xmlns:x="http://www.w3.org/1999/xhtml"
xmlns:AP="http://www.w3.org/2005/Atom"
exclude-result-prefixes="xslt msxsl user">
<xslt:output method="xml" indent="yes"/>
<xslt:template match="/">
<xslt:apply-templates select="/AP:feed//AP:entry"/>
</xslt:template>
<xslt:template match="AP:entry">
<xslt:text>Hello from entry</xslt:text>
<xslt:apply-templates select="AP:content"/>
</xslt:template>
<xslt:template match="AP:content">
<xslt:text>Hello from content</xslt:text>
<xslt:apply-templates select="x:div[@class='byline']"/>
</xslt:template>
<xslt:template match="x:div[@class='byline']">
<xslt:copy-of select="."/>
</xslt:template>
</xslt:stylesheet>
我想要做的是访问我的“div”. “Entry”和“Content”模板工作正常,因为我明确指定了命名空间.但是当我试图使用XHTML前缀(在我的情况下是“x”)访问“div”时 – XSLT看不到它.它仅在我将“div”元素添加到“AP”命名空间时才有效:
<xslt:template match="AP:content">
<xslt:text>Hello from content</xslt:text>
<xslt:apply-templates select="AP:div[@class='byline']"/>
</xslt:template>
<xslt:template match="AP:div[@class='byline']">
<xslt:copy-of select="."/>
</xslt:template>
但这对我来说并不正确,因为DIV元素应该在XHTML命名空间中.我在这做错了什么?
Atom提要在根元素上声明了Atom名称空间,没有名称空间前缀. < div />和其他XHTML元素继承Atom命名空间,因为它们没有显式声明XHTML命名空间.如果您希望XHTML元素绑定到XHTML名称空间,那么您需要更改< div>在Atom Feed中:
<div xmlns:xhtml="http://www.w3.org/1999/xhtml" class="byline">By Sheila F. Buckmaster</div>
要么:
<xhtml:div class="byline">By Sheila F. Buckmaster</xhtml:div>
如果保持Atom提要相同但仍想生成XHTML元素,则需要调整样式表以匹配AP:div,然后在输出中构造XHTML元素.
例如,修改样式表我在匹配的AP上应用模板:div在一个名为xhtml的模式中.在该模式中的任何元素上都有模板匹配(因此它也适用于AP:h2),它使用匹配元素的local-name()构造XHTML元素.
<?xml version="1.0" encoding="UTF-8"?>
<xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"
xmlns:x="http://www.w3.org/1999/xhtml"
xmlns:AP="http://www.w3.org/2005/Atom"
exclude-result-prefixes="xslt msxsl user">
<xslt:output method="xml" indent="yes"/>
<xslt:template match="/">
<xslt:apply-templates select="/AP:feed//AP:entry"/>
</xslt:template>
<xslt:template match="AP:entry">
<xslt:text>Hello from entry</xslt:text>
<xslt:apply-templates select="AP:content"/>
</xslt:template>
<xslt:template match="AP:content">
<xslt:text>Hello from content</xslt:text>
<xslt:apply-templates select="AP:div[@class='byline']"/>
</xslt:template>
<xslt:template match="AP:div[@class='byline']">
<xslt:apply-templates select="." mode="xhtml"/>
</xslt:template>
<!--create an XHTML element with the same name as the context element -->
<xslt:template match="*" mode="xhtml">
<xslt:element name="{local-name()}" namespace="http://www.w3.org/1999/xhtml">
<xslt:apply-templates select="@*|node()" mode="xhtml"/>
</xslt:element>
</xslt:template>
<!--attributes, comments, and processing-instructions simply copied -->
<xslt:template match="@*|text()|comment()|processing-instruction()">
<xslt:copy-of select="."/>
</xslt:template>
</xslt:stylesheet>
