使用xslt时如何在xml标头中包含信息

时间:2014-06-13 18:08:39

标签: xml xslt

对于xslt来说,我是一个完整的菜鸟,需要以下帮助......

我写了一个xslt,输出我想要的除了标题。这就是我想要出现的内容:

<etd_ms:thesis xmlns:etd_ms="http://www.ndltd.org/standards/metadata/etdms/1.0/"
 xsi:schemaLocation="http://www.ndltd.org/standards/metadata/etdms/1.0/ 
                     http://www.ndltd.org/standards/metadata/etdms/1.0/etdms.xsd" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

这就是我对变换的看法:

<etd_ms:thesis 
 xsi:schemaLocation="http://www.ndltd.org/standards/metadata/etdms/1.0/ 
                     http://www.ndltd.org/standards/metadata/etdms/1.0/etdms.xsd">

以下是转换本身的相关部分:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:etd_ms="http://www.ndltd.org/standards/metadata/etdms/1.0/"
            xsi:schemaLocation="http://www.ndltd.org/standards/metadata/etdms/1.0/ 
                                http://www.ndltd.org/standards/metadata/etdms/1.0/etdms.xsd">

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

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

</xsl:template>

<xsl:template match="*">
rest of code....

我如何实现我想要的目标。我虽然这条线会做我想要的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:etd_ms="http://www.ndltd.org/standards/metadata/etdms/1.0/"
            xsi:schemaLocation="http://www.ndltd.org/standards/metadata/etdms/1.0/ http://www.ndltd.org/standards/metadata/etdms/1.0/etdms.xsd">

以下是输入的示例:

<generic_etd>
  <dc.creator>Some Person</dc.creator>
  <dc.date>2006</dc.date>
  <dc.description.abstract>Long lines of text</dc.description.abstract> 
  <dc.description.note>Masters Abstracts</dc.description.note>
  <dc.format>application/pdf</dc.format>
  <dc.format>105 p.</dc.format>
  <dc.format>2.15 MB</dc.format>
  <dc.language>eng</dc.language>
  <dc.publisher>Publisher</dc.publisher>
  <dc.subject>Ecology.</dc.subject>
  <dc.title>My awesome title.</dc.title>
</generic_etd>

添加了代码here

的示例

但它没有做我想要的。我哪里出错?

1 个答案:

答案 0 :(得分:1)

让我建议以下作为转型的起点:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:etd_ms="http://www.ndltd.org/standards/metadata/etdms/1.0/">

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

<xsl:template match="/generic_etd">
    <etd_ms:thesis xsi:schemaLocation="http://www.ndltd.org/standards/metadata/etdms/1.0/ http://www.ndltd.org/standards/metadata/etdms/1.0/etdms.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <xsl:apply-templates select="@*|node()"/>
    </etd_ms:thesis>
</xsl:template>

<xsl:template match="*">
    <xsl:element name="etd_ms:{local-name()}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@*">
    <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

当上述内容应用于您的示例输入时,结果将为:

<?xml version="1.0" encoding="UTF-8"?>
<etd_ms:thesis xmlns:etd_ms="http://www.ndltd.org/standards/metadata/etdms/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ndltd.org/standards/metadata/etdms/1.0/ http://www.ndltd.org/standards/metadata/etdms/1.0/etdms.xsd">
   <etd_ms:dc.creator>Some Person</etd_ms:dc.creator>
   <etd_ms:dc.date>2006</etd_ms:dc.date>
   <etd_ms:dc.description.abstract>Long lines of text</etd_ms:dc.description.abstract>
   <etd_ms:dc.description.note>Masters Abstracts</etd_ms:dc.description.note>
   <etd_ms:dc.format>application/pdf</etd_ms:dc.format>
   <etd_ms:dc.format>105 p.</etd_ms:dc.format>
   <etd_ms:dc.format>2.15 MB</etd_ms:dc.format>
   <etd_ms:dc.language>eng</etd_ms:dc.language>
   <etd_ms:dc.publisher>Publisher</etd_ms:dc.publisher>
   <etd_ms:dc.subject>Ecology.</etd_ms:dc.subject>
   <etd_ms:dc.title>My awesome title.</etd_ms:dc.title>
</etd_ms:thesis>