简体   繁体   English

将常规XML转换为Dublin Core XML时添加名称空间前缀

[英]Adding namespace prefix when transforming regular XML into Dublin Core XML

I may very well be missing something obvious, but I'm not clear on how to code my XSLT stylesheet to transform one "regular" XML file to generate a Dublin Core XML file that includes the namespace "dc" prefix on each element. 我很可能会遗漏一些明显的东西,但是我不清楚如何编码XSLT样式表以转换一个“常规” XML文件以生成都柏林核心XML文件,该文件在每个元素上都包含名称空间“ dc”前缀。 I've looked through a lot of other answers here and can't seem to figure out how to do this. 我在这里查看了许多其他答案,但似乎无法弄清楚该如何做。 (I'm using msxsl.exe for the transformation.) (我正在使用msxsl.exe进行转换。)

For example, I'm trying to turn this line from my original XML document: 例如,我正试图从我的原始XML文档中改变这一行:

    <title>Message received clairaudiently by Mrs. Begg at Lake Pleasant, Fri. March 9, 1945.</title>

...into this (after running it through the XSLT transformation): ...(在通过XSLT转换运行它之后):

<dc:title>Message received clairaudiently by Mrs. Begg at Lake Pleasant, Fri. March 9, 1945.</dc:title>

Here's the stylesheet element I'm using in my XSL file: 这是我在XSL文件中使用的样式表元素:

    <xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xlink="http://:www.w3.org/1999/xlink" 
    xmlns:dc="http://purl.org/dc/elements/1.1/">

So my question is, do I have to hard-code the "dc:" prefix into the individual elements in the XSL stylesheet, a la: 所以我的问题是,我是否必须将“ dc:”前缀硬编码到XSL样式表中的各个元素中,例如:

<dc:title><xsl:value-of select="title" /></dc:title>

Or is there a way to have the XSL transformation add the prefix to each element automatically? 还是有办法让XSL转换自动将前缀添加到每个元素?

[I]s there a way to have the XSL transformation add the prefix to each element automatically? [I]有没有办法让XSL转换自动将前缀添加到每个元素?

If you want to prefix all elements without exceptions, you don't have to do it manually. 如果要为所有元素加上前缀而没有例外,则不必手动进行。 For example, if you have an input file like: 例如,如果您有一个输入文件,例如:

Input 输入

<?xml version="1.0"?>
<root>
    <item/>
    <item/>
</root>

apply the following transformation. 应用以下转换。 The stylesheet does not change anything except prefixing all elements with dc: . 样式表除了在dc:为所有元素添加前缀之外,不会进行任何更改。

Stylesheet 样式表

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://purl.org/dc/elements/1.1/">

   <xsl:output method="xml" indent="yes"/>
   <xsl:strip-space elements="*"/>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="*">
       <xsl:element name="{concat('dc:',name())}">
           <xsl:apply-templates select="@*|node()"/>
       </xsl:element>
   </xsl:template>

</xsl:stylesheet>

Output 产量

<?xml version="1.0" encoding="utf-8"?>
<dc:root xmlns:dc="http://purl.org/dc/elements/1.1/">
   <dc:item/>
   <dc:item/>
</dc:root>

Note that it is impossible to tell whether this can be integrated into your current stylsheet. 请注意,无法确定是否可以将其集成到当前的样式表中。 Unfortunately, you did not reveal much of it. 不幸的是,您没有透露太多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM