从 XSLT 中删除属性并使用结果集 [英] Removing Attributes from XSLT and working with result set
问题描述
是否可以从 XSLT 中删除 xml 属性并使用生成的转换?
Is it possible to remove xml attributes from XSLT AND work with the resulting transform?
换句话说,我有以下 XML:
In other words, I have the following XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="XML_TEST.xslt"?>
<report xmlns="abc123">
<book>
<page id="22">
</page>
<page id="23">
</page>
</book>
</report>
我知道我可以使用以下 XSLT 来剥离属性:
I know that I can use the following XSLT to strip the attributes:
<xsl:template match ="@*" >
<xsl:attribute name ="{local-name()}" >
<xsl:value-of select ="." />
</xsl:attribute>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match ="*" >
<xsl:element name ="{local-name()}" >
<xsl:apply-templates select ="@* | node()" />
</xsl:element>
</xsl:template>
但是如果我想读取值,请使用以下模板
But if I wanted to read the values, using the following template
<xsl:template match="report">
<xsl:for-each select="book/page">
<xsl:value-of select="@id"/>
</xsl:for-each>
</xsl:template>
我可以将该模板链接到前两个的输出吗?
Could I chain that template to the output of the first two?
提前致谢,
-R.
推荐答案
是否可以删除xml来自 XSLT 的属性并与结果转换?
Is it possible to remove xml attributes from XSLT AND work with the resulting transform?
是的,只需搜索多遍转换",您就会找到许多带有优秀代码示例的答案.
Yes, just search for "multi-pass transformation" and you'll find many answers with good code examples.
但是,对于您要执行的操作,这种转换链接过于复杂且完全没有必要.
只需使用:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="abc123" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="x:page">
<xsl:value-of select="@id"/>
</xsl:template>
</xsl:stylesheet>
如果事先不知道 XML 文档的默认命名空间,您仍然可以一次性生成所需的结果:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="*[name()='page']">
<xsl:value-of select="@id"/>
</xsl:template>
</xsl:stylesheet>
这篇关于从 XSLT 中删除属性并使用结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!