使用 XSLT 重命名 XML 元素 - IT屋-程序员软件开发技术分享社区
使用 XSLT 重命名 XML 元素
[英] Rename XML elements using XSLT
本文介绍了使用 XSLT 重命名 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要更改原始 XML 中的一些元素名称.我正在尝试使用 XSLT 执行此操作,但无法使其正常工作.
这是一个 XML 示例:
<身体><section>Jabber</section><itemtitle>JabberJabber</itemtitle><p>总是 Jabber Jabber Jabber</p><h3>大胆的请求 </h3><p>这里还有一些Jabber</p><img scr="bigpicture.jpg"></img><poll><p>哪句话最能代表你?</p></poll><pcredit>左:Jumpin Jasper/Jumpy Images</pcredit>
</html>
我需要将其更改为:
<身体><div class="issuehead">Jabber</div><div class="issuetitle">JabberJabber</div><p>总是 Jabber Jabber Jabber</p><h3>大胆的请求 </h3><p>这里还有一些Jabber</p><img scr="bigpicture.jpg"></img><div class="poll"><p>以下哪句话最能代表您?</p></div><div class="pcredit">左:Jumpin Jasper/Jumpy Images</div></html>
这是我做的 XSLT,但我无法让它工作:
<xsl:output method="html"/><xsl:template match="/"><头>头部><身体><xsl:apply-templates/></html><xsl:template match="section"><div class="issuehead"><xsl:value-of select="."/></div></xsl:模板><xsl:template match="itemtitle"><div class="issuetitle"><xsl:value-of select="."/></div></xsl:模板><xsl:template match="img"></xsl:template><xsl:template match="poll"><div class="poll"><xsl:value-of select="."/></div></xsl:模板><xsl:template match="pcredit"><div class="pcredit"><xsl:value-of select="."/></div></xsl:模板><xsl:template match="p"></xsl:template><xsl:template match="h3"></xsl:template></xsl:模板></xsl:stylesheet>
感谢您的帮助!
解决方案
对于这样的事情,从身份转换开始:
<xsl:template match="node( ) | @*"><xsl:copy><xsl:apply-templates select="@* | node( )"/></xsl:copy></xsl:模板></xsl:stylesheet>
这只会复制每个节点.但是随后您添加了其他模板来执行您需要的操作:
<div class="issuehead"><xsl:apply-templates select="@* | node( )"/></xsl:模板>
这种模式应该可以满足您的需求.
I need to change some of the element names in the original XML. I am trying to do this with XSLT, but can't get it to work.
Here is a sample of XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<html>
<body>
<section>Jabber</section>
<itemtitle>JabberJabber</itemtitle>
<p>Always Jabber Jabber Jabber</p>
<h3>Emboldened Requests </h3>
<p>Somemore Jabber Here</p>
<img scr="bigpicture.jpg"></img>
<poll><p>Which statement best characterizes you?</p></poll>
<pcredit>Left: Jumpin Jasper/Jumpy Images</pcredit>
</body>
</html>
I need to change it to:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<html>
<body>
<div class="issuehead">Jabber</div>
<div class="issuetitle">JabberJabber</div>
<p>Always Jabber Jabber Jabber</p>
<h3>Emboldened Requests </h3>
<p>Somemore Jabber Here</p>
<img scr="bigpicture.jpg"></img>
<div class="poll"><p>Which statement best characterizes you?</p></div>
<div class="pcredit">Left: Jumpin Jasper/Jumpy Images</div>
</body>
</html>
Here is the XSLT I did, but I can't get it to work:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
<xsl:template match="section">
<div class="issuehead"><xsl:value-of select="."/></div>
</xsl:template>
<xsl:template match="itemtitle">
<div class="issuetitle"> <xsl:value-of select="."/></div>
</xsl:template>
<xsl:template match="img"></xsl:template>
<xsl:template match="poll">
<div class="poll"><xsl:value-of select="."/></div>
</xsl:template>
<xsl:template match="pcredit">
<div class="pcredit"><xsl:value-of select="."/></div>
</xsl:template>
<xsl:template match="p"></xsl:template>
<xsl:template match="h3"></xsl:template>
</xsl:template>
</xsl:stylesheet>
Thanks for the help!
解决方案
For anything like this, start with an identity transform:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node( ) | @*">
<xsl:copy>
<xsl:apply-templates select="@* | node( )"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
This will just copy every node. But then you add additional templates to do what you need:
<xsl:template match="section">
<div class="issuehead">
<xsl:apply-templates select="@* | node( )"/>
</div>
</xsl:template>
This pattern should get you what you want.
这篇关于使用 XSLT 重命名 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文