xml - 由于命名空间“xmlns:ns0”,无法使用 XSLT 转换 XML
问题描述
我一直在尝试使用 XSLT 转换 XML 文件,但由于一些问题,即“xmlns:ns0”和 ns0:catalog,它没有转换。请帮我解决。
XML:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:catalog xmlns:ns0="http://sap.com">
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</ns0:catalog>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<MyCatalog>
<xsl:for-each select="catalog/cd">
<cd>
<title>
<xsl:value-of select="title" />
</title>
</cd>
</xsl:for-each>
</MyCatalog>
</xsl:template>
</xsl:stylesheet>
预期成绩:
<?xml version="1.0" encoding="UTF-8"?>
<MyCatalog>
<cd>
<title>Empire Burlesque</title>
</cd>
<cd>
<title>Hide your heart</title>
</cd>
</MyCatalog>
解决方案
您需要将命名空间添加到您的选择器:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://sap.com">
<xsl:template match="/">
<MyCatalog>
<xsl:for-each select="ns0:catalog/cd">
<cd>
<title>
<xsl:value-of select="title" />
</title>
</cd>
</xsl:for-each>
</MyCatalog>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- tensorflow - tf.keras.layers.GRU 的输入问题
- swiftui - SwiftUI Picker 视图中不显示值
- android - 更新可观察排放的回收器视图适配器
- javascript - Javascript 中的对象格式
- image - ImageMagick convert -set 注释不起作用
- javafx - 如何禁用 TextArea (JavaFX) 中的文本选择?
- javascript - 饼图中的冗余数据 - Google Scripts
- asp.net-core - Chrome 中的 adpboilerplate ASP.NET Core MVC 登录循环
- java - 为什么在使用 TinkerPop 时会出现 OutOfMemoryException?
- azure - 如何通过 ARM 模板输出返回 Redis 主键?