使用 XSLT 复制节点和属性时,当这些属性先前已由祖先节点定义时,我将丢失 xmlns 属性值。
后代的属性定义在功能上是多余的,因为它已经由祖先设置。 (如果我在那一点上错了,请纠正我。)大概这种冗余是为什么在创建后代时没有写入属性的原因,但我找不到任何东西来证实这一点。
有没有办法为那些后代保留重复/冗余的 xmlns 属性?
运行环境:
XSLT,一个简单的身份转换:
<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>
示例输入 XML,其中 4 个属性(
xmlns:oai_qdc
、xmlns:dc
、xmlns:dcterms
、xmlns:xsi
)在祖先 OAI-PMH
和后代 oai_qdc:qualifieddc
元素之间重复:
<?xml version="1.0" encoding="UTF-8"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oai_qdc="http://worldcat.org/xmlschemas/qdc-1.0/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<ListRecords>
<record>
<header>
<datestamp>1998-07-05T07:00:00Z</datestamp>
</header>
<metadata>
<oai_qdc:qualifieddc xmlns:oai_qdc="http://worldcat.org/xmlschemas/qdc-1.0/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://worldcat.org/xmlschemas/qdc-1.0/ http://worldcat.org/xmlschemas/qdc/1.0/qdc-1.0.xsd">
<dc:title>Some Title</dc:title>
</oai_qdc:qualifieddc>
</metadata>
</record>
</ListRecords>
</OAI-PMH>
转换的输出,其中 4 个重复的属性不再显示在后代
oai_qdc:qualifieddc
元素上:
<?xml version="1.0" encoding="UTF-8"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:oai_qdc="http://worldcat.org/xmlschemas/qdc-1.0/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<ListRecords>
<record>
<header>
<datestamp>1998-07-05T07:00:00Z</datestamp>
</header>
<metadata>
<oai_qdc:qualifieddc xsi:schemaLocation="http://worldcat.org/xmlschemas/qdc-1.0/ http://worldcat.org/xmlschemas/qdc/1.0/qdc-1.0.xsd">
<dc:title>Some Title</dc:title>
</oai_qdc:qualifieddc>
</metadata>
</record>
</ListRecords>
</OAI-PMH>
无论你怎么称呼属性,它们实际上都是命名空间。
是的,它们在根节点之外的任何地方都是冗余的。根节点下的每个元素都绑定到相同的命名空间。
此外,Saxon HE 11.5 支持 XSLT 3.0
因此,您的具有 Identity Transform 模式的 XSLT 可以减少为一个衬里,如下所示:
XSLT 3.0
<?xml version="1.0"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8"
omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<!--Identity Transform pattern-->
<xsl:mode on-no-match="shallow-copy"/>
</xsl:stylesheet>
要复制所有相关命名空间,请尝试以下 XSLT。
<?xml version="1.0"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8"
omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<!--Identity Transform pattern-->
<xsl:template match="node()|@*">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>