XSLT 将 XML 转换为 XML [英] XSLT Transformating XML to XML

查看:36
本文介绍了XSLT 将 XML 转换为 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 xslt 的问题.如何准确地使用模板来构建这样的输出?我已经厌倦了很长时间,使用标签xmlns有问题

I have the problem with xslt. How to exactly use template to build output like this? I get tired already a long time with this, and it is problematic to use a tag xmlns

<?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>Paul</a:name>
            <a:surname>Morgan</a:surname>
        </a:author>
        <title>Test Book</title>
    </book>
</books>

到目前为止,我能够做这样的事情:

So far I was able to do something like this:

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="http://example.net/library/1.0" >
    <xsl:template match="/">
        <books>
            <xsl:for-each select="b:library/b:books/b:book">
                <book>
                    <author>
                        <name>
                            <xsl:value-of select="../../b:authors/b:author/b:name"/>
                        </name>
                        <surname>
                            <xsl:value-of select="../../b:authors/b:author/b:surname"/>
                        </surname>
                    </author>
                    <title>
                        <xsl:value-of select="b:title"/>
                    </title>
                </book>
            </xsl:for-each>
        </books>
    </xsl:template>

</xsl:stylesheet>

起始文件是:此文件无法编辑

<?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>Chris</name>
            <surname>Pattern</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>Paul</name>
            <surname>Morgan</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>Test Book</title>
            <published>1543</published>
        </book>
    </books>
</library>

推荐答案

在准备输出记录时,您不会限制您的作者.此外 ../.. 在这种情况下也没用.使用这样的东西

You are not constraining your authors when preparing output records. Also ../.. are useless in this context. Use something like this

<xsl:value-of select="//b:authors[@id = ./@author-id ]/b:author/b:name"/>

这篇关于XSLT 将 XML 转换为 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆