我想使用XSLT转换给定的xml输入,我想要排除" xmlns"转换后的输出
中的根元素的属性我的输入xml是
<?xml version="1.0" encoding="ISO-8859-1"?>
<library xmlns="https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.2/CWLEntityExport.xsd">
<book>
<title>Programming in C</title>
<author>Balagurusamy</author>
<country>India</country>
<price>165</price>
<year>1998</year>
</book>
<book>
<title>Professional ASP.NET 4 in C# and VB</title>
<author>Bill Evjen, Scott Hanselman, Devin Rader</author>
<country>USA</country>
<price>580</price>
<year>2010</year>
</book>
<book>
<title>Professional Microsoft SQL Server 2008 Programming</title>
<author>Robert Vieira</author>
<country>USA</country>
<price>520</price>
<year>2009</year>
</book>
<book>
<title>Professional LINQ</title>
<author>Scott Klein</author>
<country>USA</country>
<price>425</price>
<year>2008</year>
</book>
</library>
使用XSLT进行转换后,和预期的xml输出如下
<?xml version="1.0" encoding="UTF-8"?>
<BookList>
<Book>
<title>Programming in C</title>
<author>Balagurusamy</author>
<country>India</country>
</Book>
<Book>
<title>Professional ASP.NET 4 in C# and VB</title>
<author>Bill Evjen, Scott Hanselman, Devin Rader</author>
<country>USA</country>
</Book>
<Book>
<title>Professional Microsoft SQL Server 2008 Programming</title>
<author>Robert Vieira</author>
<country>USA</country>
</Book>
<Book>
<title>Professional LINQ</title>
<author>Scott Klein</author>
<country>USA</country>
</Book>
</BookList>
我的xslt是
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<BookList>
<xsl:for-each select="library/book">
<Book>
<title>
<xsl:value-of select="title"/>
</title>
<author>
<xsl:value-of select="author"/>
</author>
<country>
<xsl:value-of select="country" />
</country>
</Book>
</xsl:for-each>
</BookList>
</xsl:template>
</xsl:stylesheet>
我需要在上面的XSLT中做出哪些更改才能获得给定的预期输出xml?
答案 0 :(得分:2)
我想排除&#34; xmlns&#34;来自的根元素的属性 转换输出
它不是属性 - 它是名称空间声明。它将输入XML中的所有元素放在绑定到声明的URI的名称空间中。
您需要在样式表中声明此命名空间,为其分配前缀,并在对输入XML中的元素进行寻址时使用该前缀。如果没有这个,你的XPath表达式将不会选择任何东西。
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.2/CWLEntityExport.xsd"
exclude-result-prefixes="ns1">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/ns1:library">
<BookList>
<xsl:for-each select="ns1:book">
<Book>
<title>
<xsl:value-of select="ns1:title"/>
</title>
<author>
<xsl:value-of select="ns1:author"/>
</author>
<country>
<xsl:value-of select="ns1:country" />
</country>
</Book>
</xsl:for-each>
</BookList>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:1)
我想使用XSLT转换给定的xml输入,我想要 排除&#34; xmlns&#34;转换后的根元素属性 输出
我的输入xml是
<?xml version="1.0" encoding="ISO-8859-1"?> <library xmlns="https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.2/CWLEntityExport.xsd"> <book> <title>Programming in C</title> . . .
这样做的一个非常简短,通用的方法是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:copy-of select="namespace::*[name()]"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
请注意,转换中不需要声明名称空间,也不需要特定的XPath表达式!
将此转换应用于提供的XML文档(或者,通常,在需要删除其默认命名空间的任何文档上),生成所需的正确结果:
<?xml version="1.0" encoding="utf-8"?>
<library>
<book>
<title>Programming in C</title>
<author>Balagurusamy</author>
<country>India</country>
<price>165</price>
<year>1998</year>
</book>
<book>
<title>Professional ASP.NET 4 in C# and VB</title>
<author>Bill Evjen, Scott Hanselman, Devin Rader</author>
<country>USA</country>
<price>580</price>
<year>2010</year>
</book>
<book>
<title>Professional Microsoft SQL Server 2008 Programming</title>
<author>Robert Vieira</author>
<country>USA</country>
<price>520</price>
<year>2009</year>
</book>
<book>
<title>Professional LINQ</title>
<author>Scott Klein</author>
<country>USA</country>
<price>425</price>
<year>2008</year>
</book>
</library>
<强>更新强>
如果另外需要一些元素重命名,转换成:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:copy-of select="namespace::*[name()]"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="*[name()='library']">
<BookList>
<xsl:apply-templates/>
</BookList>
</xsl:template>
<xsl:template match="*[name()='book']">
<Book>
<xsl:apply-templates/>
</Book>
</xsl:template>
<xsl:template match="*[name()='price' or name()='year']"/>
</xsl:stylesheet>
,当应用于提供的XML文档时,生成所需结果:
<?xml version="1.0" encoding="utf-8"?>
<BookList>
<Book>
<title>Programming in C</title>
<author>Balagurusamy</author>
<country>India</country>
</Book>
<Book>
<title>Professional ASP.NET 4 in C# and VB</title>
<author>Bill Evjen, Scott Hanselman, Devin Rader</author>
<country>USA</country>
</Book>
<Book>
<title>Professional Microsoft SQL Server 2008 Programming</title>
<author>Robert Vieira</author>
<country>USA</country>
</Book>
<Book>
<title>Professional LINQ</title>
<author>Scott Klein</author>
<country>USA</country>
</Book>
</BookList>