几乎是空的变换XML

时间:2016-06-30 14:13:55

标签: xslt

我是XSLT的新手,我正在寻求帮助。我需要将简单的XML文件转换为另一个XML。 这是第一个:

<?xml version="1.0" encoding="utf-8"?>
<library xmlns="http://example.net/library/1.0">
    <authors>
        <author id="a1">
            <name>John</name>
            <surname>Applesed</surname>
            <born>1979-11-11</born>
        </author>
        <author id="a2">
            <name>Krzysztof</name>
            <surname>Habdank</surname>
            <born>1965-12-12</born>
        </author>
        <author id="a3">
            <name>Paulo</name>
            <surname>Coelho</surname>
            <born>1915-06-17</born>
        </author>
        <author id="a4">
            <name>Mikołaj</name>
            <surname>Kopernik</surname>
            <born>1473-02-19</born>
            <died>1543-05-24</died>
        </author>
    </authors>
    <books>
        <book id="b1" author-id="a1">
            <title>Missing opportunity</title>
            <published>1992</published>
            <isbn>978-3-16-148410-0</isbn>
        </book>
        <book id="b2" author-id="a4">
            <title>O obrotach sfer niebieskich</title>
            <published>1543</published>
        </book>
    </books>
</library>

这是我想要的格式:

<?xml version="1.0" encoding="utf-8"?>
<books xmlns="http://example.net/books/1.0" xmlns:a="http://example.net/author/1.0">
    <book>
        <a:author>
            <a:name>John</a:name>
            <a:surname>Applesed</a:surname>
        </a:author>
        <title>Missing opportunity</title>
    </book>
    <book>
        <a:author>
            <a:name>Mikołaj</a:name>
            <a:surname>Kopernik</a:surname>
        </a:author>
        <title>O obrotach sfer niebieskich</title>
    </book>
</books>

以下是我设法编码的内容:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
  <books xmlns="http://example.net/books/1.0" xmlns:a="http://example.net/author/1.0">
     <xsl:for-each select="library/authors/author[@id='id']">
       <book>
         <name><xsl:value-of select="@name"/></name>
         <surname><xsl:value-of select="@surname"/></surname>
       </book>
     </xsl:for-each>
  </books>
</xsl:template>

</xsl:stylesheet>

不幸的是,我得到的只是:

<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://example.net/books/1.0" xmlns:a="http://example.net/author/1.0" />

我的知识来源: https://msdn.microsoft.com/en-us/library/ms766462(v=vs.85).aspx

我正在检查这个网站的效果:

http://www.freeformatter.com/xsl-transformer.html#ad-output

感谢您的时间和精力。

1 个答案:

答案 0 :(得分:0)

产生所需输出的样式表如下所示:

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:l="http://example.net/library/1.0"
    xmlns:a="http://example.net/author/1.0"
    xmlns="http://example.net/books/1.0"
    exclude-result-prefixes="l"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/l:library">
        <xsl:apply-templates select="l:books" />
    </xsl:template>

    <xsl:template match="l:books">
        <books>
            <xsl:apply-templates select="l:book" />
        </books>
    </xsl:template>

    <xsl:template match="l:book">
        <xsl:variable name="author" select="//l:author[@id = current()/@author-id]" />
        <book>
            <a:author>
                <a:name><xsl:value-of select="$author/l:name" /></a:name>
                <a:surname><xsl:value-of select="$author/l:surname" /></a:surname>
            </a:author>
            <title><xsl:value-of select="l:title" /></title>
        </book>
    </xsl:template>
</xsl:stylesheet>

注意:

  • 您打算使用的每个XML命名空间(输入或输出)都应在<xsl:stylesheet>声明。
  • 其中一个可以成为默认命名空间(此处为http://example.net/books/1.0),可以通过未注册前缀的事实来识别。样式表中的每个未加前缀的元素都将位于此命名空间中。
  • 所有其他XML名称空间都需要一个前缀,这里我为库名称空间选择l,为作者名称空间选择a,但您可以使用任何您喜欢的前缀。
  • 可以通过exclude-result-prefixes
  • 来抑制不应出现在输出文档中的所有命名空间
  • 您可以将节点存储在变量中,如$authors变量所示。
  • current()函数允许您将XPath表达式内部引用到XSLT处理器当前正在处理的节点。
  • 输入文档中默认命名空间中的节点必须使用前缀引用,例如l
  • 尝试prefer <xsl:template>/<xsl:apply-templates> when writing stylesheets
相关问题