I'm trying to figure out how XSLT process namespace prefixes and have following example: XML:
我试图找出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/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>
What I'm trying to do is to get access to my "div". "Entry" and "Content" templates work fine since I specified namespace explicitly. But when I'm trying to get access to "div" using XHTML prefix ("x" in my case) - XSLT does not see it. It works only when I prefix "div" element with "AP" namespace:
我想要做的是访问我的“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>
But this doesn't look right to me because DIV element should be in XHTML namespace. What am I doing wrong here?
但这对我来说并不正确,因为DIV元素应该在XHTML命名空间中。我在这做错了什么?
2 个解决方案
#1
2
The Atom feed has the Atom namespace declared on the root element without a namespace prefix. The <div/>
and other XHTML elements are inheriting the Atom namespace because they do not have the XHTML namespace explicitly declared.
Atom提要在根元素上声明了Atom命名空间,没有命名空间前缀。
和其他XHTML元素继承Atom命名空间,因为它们没有显式声明XHTML命名空间。If you want the XHTML elements to be bound to the XHTML namespace then you would need to change the <div>
in the Atom feed to be:
如果您希望将XHTML元素绑定到XHTML命名空间,则需要将Atom提要中的
<div xmlns:xhtml="http://www.w3.org/1999/xhtml" class="byline">By Sheila F. Buckmaster</div>
or:
<xhtml:div class="byline">By Sheila F. Buckmaster</xhtml:div>
If you keep the Atom feed the same and still want to generate XHTML elements, then you will need to adjust your stylesheet to match on AP:div
and then construct XHTML elements in the output.
如果保持Atom提要相同但仍想生成XHTML元素,则需要调整样式表以匹配AP:div,然后在输出中构造XHTML元素。
For example, modifying your stylesheet I apply-templates
on the matched AP:div
in a mode named xhtml
. There is a template matching on any element in that mode (so it would also work for the AP:h2
) that constructs XHTML elements using the local-name()
of the matched element.
例如,修改样式表我在匹配的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>
#2
2
In your xml your div would need to be xhtml:div
在你的xml你的div需要是xhtml:div
#1
2
The Atom feed has the Atom namespace declared on the root element without a namespace prefix. The <div/>
and other XHTML elements are inheriting the Atom namespace because they do not have the XHTML namespace explicitly declared.
Atom提要在根元素上声明了Atom命名空间,没有命名空间前缀。
和其他XHTML元素继承Atom命名空间,因为它们没有显式声明XHTML命名空间。If you want the XHTML elements to be bound to the XHTML namespace then you would need to change the <div>
in the Atom feed to be:
如果您希望将XHTML元素绑定到XHTML命名空间,则需要将Atom提要中的
<div xmlns:xhtml="http://www.w3.org/1999/xhtml" class="byline">By Sheila F. Buckmaster</div>
or:
<xhtml:div class="byline">By Sheila F. Buckmaster</xhtml:div>
If you keep the Atom feed the same and still want to generate XHTML elements, then you will need to adjust your stylesheet to match on AP:div
and then construct XHTML elements in the output.
如果保持Atom提要相同但仍想生成XHTML元素,则需要调整样式表以匹配AP:div,然后在输出中构造XHTML元素。
For example, modifying your stylesheet I apply-templates
on the matched AP:div
in a mode named xhtml
. There is a template matching on any element in that mode (so it would also work for the AP:h2
) that constructs XHTML elements using the local-name()
of the matched element.
例如,修改样式表我在匹配的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>
#2
2
In your xml your div would need to be xhtml:div
在你的xml你的div需要是xhtml:div