【发布时间】:2019-03-16 04:05:30
【问题描述】:
您好,我有一个 xml 文件如下:
<root>
<body>
<place attr="1" id="1" ref="www.example.com">
<name>abc
</name>
</place>
<place attr="1" id="2" ref="www.example.com">
<name>def
</name>
</place>
<place attr="2" id="3">
<place attr="3" id="4" ref="www.example.com">
<name>efg
</name>
</place>
</place>
</body>
</root>
我想获取所有元素 <place> 有一个子 <name> 并且元素之间没有文本。
我想要的输出是这样的:
<root>
<place attr="1" id="1" ref="www.example.com" />
<place attr="1" id="2" ref="www.example.com" />
<place attr="3" id="4" ref="www.example.com" />
</root>
我的 xsl 代码返回所有 <place> 标签(我不想要)+ <name> 标签之间的文本:
<xsl:template match="root">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="place">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
xml 输出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<place attr="1" id="1" ref="www.example.com">abc</place>
<place attr="1" id="2" ref="www.example.com">def</place>
<place attr="2" id="3">
<place attr="3" id="4" ref="www.example.com">efg</place>
</place>
</root>
【问题讨论】:
-
那么你到底在哪里坚持这个?
-
请发布您的 XSLT 代码。正如迈克尔指出的那样,如果我们不知道您的具体问题是什么,我们将无法有效地帮助您。 “我要XXX”不是问题,是请求。
-
我这样做了:
xml <xsl:template match="place"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:apply-templates/> </xsl:copy> </xsl:template>我得到了所有带有文本的标记,但无法得到我的示例中提到的 标记后面的那些 -
请使用您的工作 XSL 代码更新您的帖子。理想情况下,如果您提供minimal, complete, and verifiable example,我们将能够为您提供最好的帮助。
-
是的,我会的,忘记添加谢谢。