我正在尝试使用XSLT将XML文档转换为非常相似的XML文档,但还有一些附加功能。我无法让xsl:copy-of正常工作。当我尝试转换以下示例XML文档时:
<?xml version="1.0" encoding="UTF-8"?>
<mods xmlns="http://www.loc.gov/mods/v3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:mods="http://www.loc.gov/mods/v3"
xsi:schemaLocation="http://www.loc.gov/mods/v3
http://www.loc.gov/standards/mods/v3/mods-3-4.xsd">
<titleInfo>
<title>Test title
</title>
</titleInfo>
<subject authority="naf">
<geographic>Geo subject</geographic>
</subject>
<location>
<physicalLocation>Location here</physicalLocation>
</location>
<originInfo>
<dateCreated keyDate="yes">1904-01-05</dateCreated><dateCreated/>
</originInfo>
<typeOfResource>text</typeOfResource>
<genre authority="aat" valueURI="300026880">correspondence</genre>
<physicalDescription>
<extent>3 pages.</extent>
<note type="physical description">All pages ripped down the
middle.
</note>
</physicalDescription>
<relatedItem type="host" displayLabel="Collection"
<titleInfo>
<title>Collection name</title>
</titleInfo>
</relatedItem>
<accessCondition type="use and reproduction" displayLabel="Use and
Reproduction">Access condition here</accessCondition>
<identifier type="local">IDID</identifier>
</mods>
使用以下XSLT,仅XSLT中的文字值(originInfo,accessCondition)在结果XML文档中输出。我不知道为什么会这样。当我从源XML中删除所有标头信息时,转换确实起作用。但是我所有的XML文件都具有该标头,并且我想使XSLT可以使用它-我的猜测是我的名称空间声明相互矛盾,但是我不知道为什么。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xlink="https://www.w3.org/1999/xlink"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mods="http://www.loc.gov/mods/v3" version="2.0" exclude-
result-prefixes="mods">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<mods>
<xsl:copy-of select="mods/titleInfo"/>
<xsl:copy-of select="mods/typeOfResource"/>
<xsl:copy-of select="mods/location"/>
<xsl:copy-of select="mods/physicalDescription"/>
<xsl:copy-of select="mods/subject"/>
<xsl:copy-of select="mods/name"/>
<xsl:copy-of select="mods/identifier"/>
<xsl:copy-of select="mods/genre"/>
<xsl:copy-of select="mods/relatedItem"/>
<xsl:copy-of select="mods/accessCondition"/>
<xsl:copy-of select="mods/language"/>
<xsl:copy-of select="mods/abstract"/>
<xsl:copy-of select="mods/note"/>
<originInfo>
<dateCreated>
<xsl:value-of select="mods/originInfo/dateCreated"/>
</dateCreated>
<dateCreated encoding="w3cdtf" keyDate="yes"
point="start">
<xsl:value-of select="mods/originInfo/dateCreated"/>
</dateCreated>
</originInfo>
<accessCondition type="use and reproduction">
<xsl:text>Copyright statement here</xsl:text>
</accessCondition>
</mods>
</xsl:template>
我的预期输出是:
<?xml version="1.0" encoding="UTF-8"?>
<mods xmlns="http://www.loc.gov/mods/v3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:mods="http://www.loc.gov/mods/v3"
xsi:schemaLocation="http://www.loc.gov/mods/v3
http://www.loc.gov/standards/mods/v3/mods-3-4.xsd">
<titleInfo>
<title>Test title
</title>
</titleInfo>
<subject authority="naf">
<geographic>Geo subject</geographic>
</subject>
<location>
<physicalLocation>Location here</physicalLocation>
</location>
<typeOfResource>text</typeOfResource>
<genre authority="aat" valueURI="300026880">correspondence</genre>
<physicalDescription>
<extent>3 pages.</extent>
<note type="physical description">All pages ripped down the
middle.
</note>
</physicalDescription>
<relatedItem type="host" displayLabel="Collection"
<titleInfo>
<title>Collection name</title>
</titleInfo>
</relatedItem>
<accessCondition type="use and reproduction" displayLabel="Use and
Reproduction">Access condition here</accessCondition>
<identifier type="local">IDID</identifier>
<originInfo>
<dateCreated>1904-01-05 </dateCreated>
<dateCreated encoding="w3cdtf" keyDate="yes" point="start">1904-01-05 </dateCreated>
</originInfo>
<accessCondition type="use and reproduction">Copyright statement here</accessCondition>
</mods>
答案 0 :(得分:1)
您的XSLT存在两个问题:
它不会在XML输入中选择任何内容,因为您的XML输入会将其节点放在命名空间中。如果您使用的是XSLT 2.0,则可以通过在xpath-default-namespace="http://www.loc.gov/mods/v3"
开头标记中加入xsl:stylesheet
来解决此问题。
它不会将文字结果元素放在目标名称空间中。为此,您必须将目标名称空间声明为更高级别节点之一中的默认名称空间,例如通过在您的xmlns="http://www.loc.gov/mods/v3"
开头标记中加入xsl:stylesheet
。
此外,为了防止将原始名称空间声明复制到样式表复制的每个元素中,最好将文本结果元素<mods>
替换为原始结果的副本。
这是包含以下更改的完整样式表:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.loc.gov/mods/v3"
xpath-default-namespace="http://www.loc.gov/mods/v3">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/mods">
<xsl:copy>
<xsl:copy-of select="titleInfo"/>
<xsl:copy-of select="typeOfResource"/>
<xsl:copy-of select="location"/>
<xsl:copy-of select="physicalDescription"/>
<xsl:copy-of select="subject"/>
<xsl:copy-of select="name"/>
<xsl:copy-of select="identifier"/>
<xsl:copy-of select="genre"/>
<xsl:copy-of select="relatedItem"/>
<xsl:copy-of select="accessCondition"/>
<xsl:copy-of select="language"/>
<xsl:copy-of select="abstract"/>
<xsl:copy-of select="note"/>
<originInfo>
<dateCreated>
<xsl:value-of select="originInfo/dateCreated"/>
</dateCreated>
<dateCreated encoding="w3cdtf" keyDate="yes" point="start">
<xsl:value-of select="originInfo/dateCreated"/>
</dateCreated>
</originInfo>
<accessCondition type="use and reproduction">
<xsl:text>Copyright statement here</xsl:text>
</accessCondition>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>