【问题标题】:Join XML nodes with XSL使用 XSL 连接 XML 节点
【发布时间】:2016-10-12 16:16:48
【问题描述】:

如何使用 docid 字段将 /Document/Head/Signature 节点与对应的 /Document/Image 节点连接,以便在同一块中将内容输出到 HTML?

<?xml version="1.0"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Head>
        <Account>Fred123</Account>
        <accountName>Fred Blogs Ltd</accountName>
        <Signature>
            <sigName>Fred Bloggs</sigName>
            <docid>39215896554.0</docid>
        </Signature>
    </Head>
    <Image>
        <docid>39215896554.0</docid>
        <docTitle>Fred Bloggs Signature</docTitle>
    </Image>
    <Image>
        <docid>121212121212.0</docid>
        <docTitle>Jo Smith Signature</docTitle>
    </Image>
</Document>

样本输出:

<div id="sig">
    Signature Name: Fred Bloggs<br />
    Signature Title: Fred Bloggs Signature
</div>

我已经尝试了几种使用xsl:for-each 的方法,但有些地方不太对劲......这是一次尝试:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <html>
        <head>
        </head>
        <body>
            <h1>Page</h1>
            <xsl:for-each select="/Document/Head/Signature[docid = /Document/Image/docid]">
                <h4><xsl:value-of select="sigName"/></h4>
                <h4><xsl:value-of select="docTitle"/></h4>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    你有正确的条件,显然然后要从被引用的元素输出数据,你需要再次引用它:

    <xsl:template match="/">
        <html>
            <head>
            </head>
            <body>
                <h1>Page</h1>
                <xsl:for-each select="/Document/Head/Signature[docid = /Document/Image/docid]">
                    <h4><xsl:value-of select="sigName"/></h4>
                    <h4><xsl:value-of select="/Document/Image[docid = current()/docid]/docTitle"/></h4>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
    

    我会用钥匙

    <xsl:key name="image-ref" match="Image" use="docid"/>
    <xsl:template match="/">
        <html>
            <head>
            </head>
            <body>
                <h1>Page</h1>
                <xsl:for-each select="/Document/Head/Signature[key('image-ref', docid)]">
                    <h4><xsl:value-of select="sigName"/></h4>
                    <h4><xsl:value-of select="key('image-ref', docid)/docTitle"/></h4>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 2011-02-27
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多