如何定位命名空间 xsi:type= "test1"



下面是将用于转换的示例输入XML文件。需要是转换后的XML输出

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test2">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test2">
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>

我需要编写一个XSL转换器,这样我只能遍历具有xsi:type="test1">

的catalog/cd输出XML应该如下所示

<catalog>
<cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
</catalog>

任何帮助都非常感谢。

欢呼

请尝试以下解决方案。

XSLT遵循所谓的恒等变换模式。

输入XML

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test2">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test2">
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>
XSLT>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="cd[@xsi:type!='test1']"/>
</xsl:stylesheet>

输出XML

<catalog>
<cd xsi:type="test1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd xsi:type="test1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
</catalog>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd[@xsi:type='test2']">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

解决

最新更新