如何重命名导入的 XML 节点
How to rename imported XML node
我需要重命名导入的 XML 节点名称。
下面是我在 XSL 中创建新节点名称的尝试,但我最终得到了 XML 节点名称作为子节点。
我的XML节点:
<?xml version="1.0" encoding="UTF-8"?>
<offices:sales
xmlns="http://www.w3.org/1999/xhtml"
xmlns:backend="http://www.example/1"
xmlns:offices="http://www.example/2"
xmlns:ix="http://www.example/3"
>
<backend:YearlySales name="sales_2021" attribute_1="1">1000</backend:YearlySales>
</offices:sales>
我的 XSL 代码副本:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:backend="http://www.example/1"
xmlns:offices="http://www.example/2"
xmlns:ix="http://www.example/3"
>
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<html>
<body>
<ix:base>
<xsl:copy-of copy-namespaces="no" select="offices:sales/backend:YearlySales">
</xsl:copy-of>
</ix:base>
</body>
</html>
</xsl:template>
</xsl:transform>
结果
<!DOCTYPE html
PUBLIC "XSLT-compat">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:backend="http://www.example/1" xmlns:offices="http://www.example/2" xmlns:ix="http://www.example/3">
<body>
<ix:base>
<backend:YearlySales name="sales_2021" attribute_1="1">1000</backend:YearlySales>
</ix:base>
</body>
</html>
想要的结果:
<!DOCTYPE html
PUBLIC "XSLT-compat">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:backend="http://www.example/1" xmlns:offices="http://www.example/2" xmlns:ix="http://www.example/3">
<body>
<ix:base name="sales_2021" attribute_1="1">1 000</ix:base>
</body>
</html>
我想这会产生你需要的东西:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:backend="http://www.example/1"
xmlns:offices="http://www.example/2"
xmlns:ix="http://www.example/3">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<html>
<body>
<ix:base>
<xsl:copy-of select="offices:sales/backend:YearlySales/@*"/>
<xsl:value-of select="offices:sales/backend:YearlySales"/>
</ix:base>
</body>
</html>
</xsl:template>
</xsl:transform>
我需要重命名导入的 XML 节点名称。 下面是我在 XSL 中创建新节点名称的尝试,但我最终得到了 XML 节点名称作为子节点。
我的XML节点:
<?xml version="1.0" encoding="UTF-8"?>
<offices:sales
xmlns="http://www.w3.org/1999/xhtml"
xmlns:backend="http://www.example/1"
xmlns:offices="http://www.example/2"
xmlns:ix="http://www.example/3"
>
<backend:YearlySales name="sales_2021" attribute_1="1">1000</backend:YearlySales>
</offices:sales>
我的 XSL 代码副本:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:backend="http://www.example/1"
xmlns:offices="http://www.example/2"
xmlns:ix="http://www.example/3"
>
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<html>
<body>
<ix:base>
<xsl:copy-of copy-namespaces="no" select="offices:sales/backend:YearlySales">
</xsl:copy-of>
</ix:base>
</body>
</html>
</xsl:template>
</xsl:transform>
结果
<!DOCTYPE html
PUBLIC "XSLT-compat">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:backend="http://www.example/1" xmlns:offices="http://www.example/2" xmlns:ix="http://www.example/3">
<body>
<ix:base>
<backend:YearlySales name="sales_2021" attribute_1="1">1000</backend:YearlySales>
</ix:base>
</body>
</html>
想要的结果:
<!DOCTYPE html
PUBLIC "XSLT-compat">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:backend="http://www.example/1" xmlns:offices="http://www.example/2" xmlns:ix="http://www.example/3">
<body>
<ix:base name="sales_2021" attribute_1="1">1 000</ix:base>
</body>
</html>
我想这会产生你需要的东西:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:backend="http://www.example/1"
xmlns:offices="http://www.example/2"
xmlns:ix="http://www.example/3">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<html>
<body>
<ix:base>
<xsl:copy-of select="offices:sales/backend:YearlySales/@*"/>
<xsl:value-of select="offices:sales/backend:YearlySales"/>
</ix:base>
</body>
</html>
</xsl:template>
</xsl:transform>