代码之家  ›  专栏  ›  技术社区  ›  Chris Huang-Leaver

如何使用xslt添加“join”xml文件?

  •  1
  • Chris Huang-Leaver  · 技术社区  · 14 年前

    这是我真正问题的简化版本,但我已经测试过了,得到了相同的结果;

    <?xml version="1.0" encoding="UTF8"?>
    <fieldList>
    <sourceField name="SourceTime">
    <fid>REC_TIME</fid>
    </sourceField>
    </fieldList>
    

    查找.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <data>
    <field  name="REC_TIME" fid_type="DATE"/>
    </data>
    

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="UTF-8" media-type="text/plain"/>
    <xsl:variable name="fieldDict" select="'lookup.xml'" />
    <xsl:template match="/fieldList/*[fid]">
    <xsl:variable name="id" select="current()"/>
    <xsl:value-of select="$id"/>
    <xsl:for-each select="document($fieldDict)/data/field[@name = $id]">
    <xsl:value-of select="@type"/>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
    

    现在,如果我将xpath改为[@name],我将得到

    REC_TIME
    MONKEY
    

    我在Linux上用xsltproc测试这个

    xsltproc --version
    Using libxml 20705, libxslt 10124 and libexslt 813
    xsltproc was compiled against libxml 20632, libxslt 10124 and libexslt 813
    libxslt 10124 was compiled against libxml 20632
    libexslt 813 was compiled against libxml 20632
    

    1 回复  |  直到 14 年前
        1
  •  2
  •   Dimitre Novatchev    14 年前

    这种转换是对XSLT代码的最简单的更正,它可以产生所需的结果 :

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="my:my"
    >
        <xsl:output method="xml" encoding="UTF-8" media-type="text/plain"/>
    
        <my:lookup>
            <data>
              <field  name="REC_TIME" fid_type="DATE"/>
            </data>
        </my:lookup>
    
        <xsl:variable name="fieldDict" select="document('')/*/my:lookup" />
    
        <xsl:template match="/fieldList/*[fid]">
            <xsl:variable name="id" select="fid"/>
            <xsl:value-of select="$id"/>
            <xsl:text> </xsl:text>
            <xsl:for-each select="$fieldDict/data/field[@name = $id]">
                <xsl:value-of select="@fid_type"/>
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
    

    :

    <fieldList>
        <sourceField name="SourceTime">
            <fid>REC_TIME</fid>
        </sourceField>
    </fieldList>
    

    得到想要的结果 :

    REC_TIME DATE
    

    做笔记

    1. 对代码的唯一重大更改是 $id .

    2. .