How to use attribute values from another XML file as an element value selection in the current XML

前端 未结 3 1136
北恋
北恋 2021-01-07 08:40

I have two XML files. One is the main XML file and the other one is used as a lookup table. Here is the main XML:


      
                   


        
相关标签:
3条回答
  • 2021-01-07 09:13

    Maybe something like this would be simpler:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
    
        <xsl:variable name="lookup" select="document('lookup.xml')/lookup/fieldmapping" />
    
        <xsl:template match="/">
            <Items>
                <xsl:apply-templates/>
            </Items>
        </xsl:template>
    
        <xsl:template match="row">
            <Item>
                <xsl:for-each select="./*">
                    <xsl:variable name="this" select="local-name()"/>
                    <xsl:variable name="name" select="$lookup[@name=$this]"/>
                    <FieldName name="{$name}">
                        <xsl:copy-of select="./node()" />
                    </FieldName>
                </xsl:for-each>
            </Item>
        </xsl:template>
    
    </xsl:stylesheet>
    
    0 讨论(0)
  • 2021-01-07 09:16

    I would do it like this (assuming XSLT 2.0):

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">
    
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:key name="k1" match="fieldmapping" use="@name"/>
    
      <xsl:variable name="lookupDoc" select="doc('lookup.xml')"/>
    
      <xsl:template match="Report">
        <Items>
          <xsl:apply-templates/>
        </Items>
      </xsl:template>
    
      <xsl:template match="row">
        <Item>
          <xsl:apply-templates/>
        </Item>
      </xsl:template>
    
      <xsl:template match="row/*">
        <fieldName name="{key('k1', local-name(), $lookupDoc)}">
          <xsl:value-of select="."/>
        </fieldName>
      </xsl:template>
    
    </xsl:stylesheet>
    

    [edit] Here is an XSLT 1.0 rewrite of the above XSLT 2.0 stylesheet:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
    
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:key name="k1" match="fieldmapping" use="@name"/>
    
      <xsl:variable name="lookupDoc" select="document('lookup.xml')"/>
    
      <xsl:template match="Report">
        <Items>
          <xsl:apply-templates/>
        </Items>
      </xsl:template>
    
      <xsl:template match="row">
        <Item>
          <xsl:apply-templates/>
        </Item>
      </xsl:template>
    
      <xsl:template match="row/*">
        <xsl:variable name="this" select="."/>
        <xsl:variable name="lookup">
          <xsl:for-each select="$lookupDoc">
            <xsl:value-of select="key('k1', local-name($this))"/>
          </xsl:for-each>
        </xsl:variable>
        <fieldName name="{$lookup}">
          <xsl:value-of select="."/>
        </fieldName>
      </xsl:template>
    
    </xsl:stylesheet>
    
    0 讨论(0)
  • 2021-01-07 09:17

    This is the way I would do it (although I like Martin's use of xsl:key):

    I modified main.xml and lookup.xml to show the dynamic name lookup.

    main.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Report>
      <row>
        <field1>test1</field1>
        <field2>test2</field2>
        <field3>test3</field3>
        <newfield>testing new element</newfield>
      </row>
      <row>
        <field1>test4</field1>
        <field2>test5</field2>
        <field3>test6</field3>
        <newfield>testing new element again</newfield>
      </row>
    </Report>
    

    lookup.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <lookup>
      <fieldmapping name="field1">fieldA</fieldmapping>
      <fieldmapping name="field2">fieldB</fieldmapping>
      <fieldmapping name="field3">fieldC</fieldmapping>
      <fieldmapping name="newfield">fieldD</fieldmapping>
    </lookup>
    

    XSLT 1.0 stylesheet

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes" method="xml"/>
    
      <xsl:variable name="mappingLookupDoc" select="document('lookup.xml')"/>
    
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="Report">
        <Items>
          <xsl:apply-templates/>
        </Items>
      </xsl:template>
    
      <xsl:template match="row">
        <Item>
          <xsl:apply-templates/>
        </Item>    
      </xsl:template>
    
      <xsl:template match="*[name() = $mappingLookupDoc/lookup/fieldmapping/@name]">
        <xsl:variable name="elemName" select="name()"/>
        <FieldName name="{$mappingLookupDoc/lookup/fieldmapping[@name=$elemName]}">
          <xsl:apply-templates/>
        </FieldName>
      </xsl:template>
    
    </xsl:stylesheet>
    

    output

    <?xml version="1.0" encoding="UTF-8"?>
    <Items>
      <Item>
          <FieldName name="fieldA">test1</FieldName>
          <FieldName name="fieldB">test2</FieldName>
          <FieldName name="fieldC">test3</FieldName>
          <FieldName name="fieldD">testing new element</FieldName>
      </Item>
      <Item>
          <FieldName name="fieldA">test4</FieldName>
          <FieldName name="fieldB">test5</FieldName>
          <FieldName name="fieldC">test6</FieldName>
          <FieldName name="fieldD">testing new element again</FieldName>
      </Item>
    </Items>
    
    0 讨论(0)
提交回复
热议问题