如何使用 XSLT 将节点集复制到结果属性中,而无需编码空格和标签?
给定这个 XML...
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<this>
<that>one</that>
</this>
</item>
<item>
<this>
<that>two</that>
</this>
</item>
<item>
<this>
<that>three</that>
</this>
</item>
</root>
我想将项目的副本制作成新的格式,看起来像...
<new>
<parm x=">that<one>/that<"/>
<parm x=">that<two>/that<"/>
<parm x=">that<three>/that<"/>
</new>
样式表...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="feedKey"/>
<xsl:template match="/">
<new>
<xsl:apply-templates select="//item"/>
</new>
</xsl:template>
<xsl:template match="item">
<xsl:element name="param">
<xsl:attribute name="x"><xsl:copy-of select="node()"/></xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
...产生...
<?xml version="1.0" encoding="UTF-8"?>
<new>
<param x="
 
one
 
 "/>
<param x="
 
two
 
 "/>
<param x="
 
three
 
 "/>
</new>
对表进行简单的更改以删除属性...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="feedKey"/>
<xsl:template match="/">
<new>
<xsl:apply-templates select="//item"/>
</new>
</xsl:template>
<xsl:template match="item">
<xsl:element name="param">
<xsl:copy-of select="node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
...产生...
<?xml version="1.0" encoding="UTF-8"?>
<new>
<param>
<this>
<that>one</that>
</this>
</param>
<param>
<this>
<that>two</that>
</this>
</param>
<param>
<this>
<that>three</that>
</this>
</param>
</new>
如何将“this”转换为属性“x”,并删除空格并对标签进行编码?
Given this XML...
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<this>
<that>one</that>
</this>
</item>
<item>
<this>
<that>two</that>
</this>
</item>
<item>
<this>
<that>three</that>
</this>
</item>
</root>
I want to make copies of the items into a new format which looks like...
<new>
<parm x=">that<one>/that<"/>
<parm x=">that<two>/that<"/>
<parm x=">that<three>/that<"/>
</new>
The style sheet...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="feedKey"/>
<xsl:template match="/">
<new>
<xsl:apply-templates select="//item"/>
</new>
</xsl:template>
<xsl:template match="item">
<xsl:element name="param">
<xsl:attribute name="x"><xsl:copy-of select="node()"/></xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
...produces...
<?xml version="1.0" encoding="UTF-8"?>
<new>
<param x="
one
"/>
<param x="
two
"/>
<param x="
three
"/>
</new>
A simple change to the sheet to remove the attribute...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="feedKey"/>
<xsl:template match="/">
<new>
<xsl:apply-templates select="//item"/>
</new>
</xsl:template>
<xsl:template match="item">
<xsl:element name="param">
<xsl:copy-of select="node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
...produces...
<?xml version="1.0" encoding="UTF-8"?>
<new>
<param>
<this>
<that>one</that>
</this>
</param>
<param>
<this>
<that>two</that>
</this>
</param>
<param>
<this>
<that>three</that>
</this>
</param>
</new>
How can I convert "this" into attribute "x" with the white space stripped and the tags encoded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此转换:
当应用于提供的 XML 文档时:
产生所需的结果:
This transformation:
when applied on the provided XML document:
produces the desired result:
据我所知,如果我正确理解你的问题; XSLT 中没有提供执行此操作的方法。我的假设是 W3C 认为存储转义 XML 是一种反模式。我的方法是定义一个命名模板:
等等。我希望您能够理解我在这里尝试做的事情:使用上下文节点的名称以及每个属性的名称和值手动构造一个开始标记,然后迭代上下文节点的每个子节点并调用相同的模板再次;等等。我没有原始的实现可以交给这里,所以毫无疑问我的例子有一些问题;这只是一个指南。
如果有人知道更好的方法,我想听听;它可能位于尚未正式化的 XSLT 2.0 中,IIRC。为了实现稳健性和 MSXML 支持,它必须是 XSLT 1.0。
To my knowledge and if I understand your question correctly; there isn't a way of doing this included in XSLT. My assumption is that W3C consider storing escaped XML to be an anti-pattern. My approach was to define a named template:
And so on. I hope you can decipher what I'm trying to do here: construct manually an opening tag with the name of the context node and the name and value of each attribute, then iterate through every child node of the context node and call the same template again; and so on. I don't have my original implementation to hand here so no doubt there's a few things wrong with my example; it's just a guide.
If anybody knows of a better way I'd like to hear it; it may be in XSLT 2.0 which isn't formalised yet, IIRC. For robustness and MSXML support, it's going to have to be XSLT 1.0.