Xml 如何使用XSLT将节点集复制到结果属性中,而不使用空格和编码的标记?
给定此XMLXml 如何使用XSLT将节点集复制到结果属性中,而不使用空格和编码的标记?,xml,xslt,encoding,Xml,Xslt,Encoding,给定此XML <?xml version="1.0" encoding="UTF-8"?> <root> <item> <this> <that>one</that> </this> </item> <item> <this> <that>two</that> <
<?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"?>
<new>
<param>
<this>
<that>one</that>
</this>
</param>
<param>
<this>
<that>two</that>
</this>
</param>
<param>
<this>
<that>three</that>
</this>
</param>
</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 x="
 
one
 
 "/>
<param x="
 
two
 
 "/>
<param x="
 
three
 
 "/>
</new>
<?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”,去掉空白并对标记进行编码?据我所知,如果我正确理解了您的问题;XSLT中没有实现这一点的方法。我的假设是W3C考虑将漏掉的XML存储为反模式。我的方法是定义一个命名模板:
<xsl:template name='recursive_escape'>
<xsl:text>;<</xsl:text>
<xsl:value-of select='local-name()'/>
<xsl:for-each select='child::attribute()'>
<xsl:value-of select='local-name()'/>
<xsl:text>='</xsl:text><xsl:value-of select='.'/><xsl:text>'</xsl:text>
</xsl:for-each>
<xsl:text>/;></xsl:text>....
;
=''
/;....
等等。我希望您能理解我在这里要做的事情:用上下文节点的名称以及每个属性的名称和值手动构造一个开始标记,然后遍历上下文节点的每个子节点,并再次调用相同的模板;等等我手头没有我最初的实现,所以毫无疑问我的示例有一些地方不对劲;这只是一个指南
如果有人知道更好的方法,我愿意听;它可能出现在XSLT2.0中,它还没有正式化,IIRC。对于健壮性和MSXML支持,它必须是XSLT1.0。此转换:
<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="/">
<new>
<xsl:apply-templates select="*/*/this/*"/>
</new>
</xsl:template>
<xsl:template match="*">
<xsl:variable name="vStr">
<xsl:text><</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
<xsl:apply-templates/>
<xsl:text></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</xsl:variable>
<parm x="{$vStr}"/>
</xsl:template>
</xsl:stylesheet>
/
应用于提供的XML文档时:
<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>
一
二
三
产生所需的结果:
<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>
好问题(+1)。有关完整的解决方案,请参见我的答案。:)