[英]Get all tags with a specific name independent of element-level
I'm creating html with PHP (simpleXML), a XML-document and XSL. 我正在用PHP(simpleXML),一个XML文档和XSL创建html。
Is there a way to replace all elements with a specific name (ie. ) with a html-tag (ie. ). 有没有办法用html标签(即)替换具有特定名称(即)的所有元素。 I guess the answer is 'yes", but how do I do it?
我猜答案是“是”,但是我该怎么办?
With my code, the keyword-element must be the top child of the root-element for it to work. 在我的代码中,关键字元素必须是根元素的顶级子元素才能起作用。
The following don't work: 以下无效:
XML: XML:
<document>
<chapter>This is the first chapter</chapter>
<text>This is a text, with a <keyword>keyword</keyword></text>
</document>
XSL: XSL:
<xsl:template match="text">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'keyword')]">
<xsl:copy>
<b>
<xsl:value-of select="."/>
</b>
</xsl:copy>
</xsl:template>
The <keyword>
-element can exist on all kinds of levels in the document, lets say even in the title. <keyword>
-element可以存在于文档的各个级别,甚至可以说是标题。 How can I then select it from the XSL? 然后如何从XSL中选择它? I guess there is something to do with the 'match'-attribute.
我猜想这与“匹配”属性有关。 I tried match="*/keyword" without any luck.
我尝试过match =“ * / keyword”却没有任何运气。
This code works when the <keyword>
-element is the top child of the root, but won't work inside for example the <text>
-element. 当
<keyword>
-element是根的顶级子级时,此代码有效,但在<text>
-element等内部将不起作用。
I have modified your input example to: 我已将您的输入示例修改为:
<document>
<title>This is the <keyword>first</keyword> title</title>
<chapter>This is the <keyword>second</keyword> chapter</chapter>
<text>This is a text, with a <keyword>third</keyword> keyword in it.</text>
</document>
Using the following stylesheet: 使用以下样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<!-- Identity Transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- exception -->
<xsl:template match="keyword">
<b><xsl:apply-templates/></b>
</xsl:template>
</xsl:stylesheet>
you will get the following output: 您将获得以下输出:
<?xml version="1.0" encoding="utf-8"?>
<document>
<title>This is the <b>first</b> title</title>
<chapter>This is the <b>second</b> chapter</chapter>
<text>This is a text, with a <b>third</b> keyword in it.</text>
</document>
I'm not total sure what you are trying to do, but the following query: 我不确定您要做什么,但是可以查询以下查询:
<xsl:value-of select="//keyword/text()"/>
will select all <keyword>
element's content regardless of their position in tree. 会选择所有
<keyword>
元素的内容,无论它们在树中的位置如何。 This because I'm using the //
in front of 这是因为我在前面使用
//
I think what you are looking for is on this question . 我认为您正在寻找的是这个问题 。
Basically, the */keyword
is looking for a string that has "/keyword" in it, and not the actual node. 基本上,
*/keyword
是在其中查找包含“ / keyword”的字符串,而不是实际的节点。
If you try 如果你试试
<xsl:template match="*[starts-with(name(), 'keyword')]">
You should be in good shape. 您应该身体状况良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.