gpt4 book ai didi

xml - XSLT 和 XHTML 输出的默认命名空间

转载 作者:数据小太阳 更新时间:2023-10-29 02:06:53 24 4
gpt4 key购买 nike

我试图弄清楚 XSLT 如何处理 namespace 前缀并有以下示例: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”。 “条目”和“内容”模板工作正常,因为我明确指定了命名空间。但是当我尝试使用 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 提要在没有 namespace 前缀的根元素上声明了 Atom namespace 。 <div/>和其他 XHTML 元素正在继承 Atom 命名空间,因为它们没有显式声明 XHTML 命名空间。

如果您希望将 XHTML 元素绑定(bind)到 XHTML namespace ,那么您需要更改 <div>在 Atom 提要中:

<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 元素。

例如,修改样式表 I apply-templates在匹配的 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>

关于xml - XSLT 和 XHTML 输出的默认命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9762005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com