如何使用另一个 XML 文件中的属性值作为当前 XML 中的元素值选择

发布于 2024-10-17 03:25:19 字数 2560 浏览 1 评论 0原文

我有两个 XML 文件。一个是主 XML 文件,另一个用作查找表。这是主要的 XML:

<Report>
      <row>
            <field1>test1</field1>
            <field2>test2</field2>
            <field3>test3</field3>
      </row>
      <row>
            <field1>test4</field1>
            <field2>test5</field2>
            <field3>test6</field3>
      </row>
</Report>

查找 xml 文件如下所示:

<lookup>
      <fieldmapping name="field1">fieldA</fieldmapping>
      <fieldmapping name="field2">fieldB</fieldmapping>
      <fieldmapping name="field3">fieldC</fieldmapping>
</lookup>

这是我想要的输出 xml:

<Items>
      <Item>
           <FieldName name="fieldA">test1</FieldName>
           <FieldName name="fieldB">test2</FieldName>
           <FieldName name="fieldC">test3</FieldName>
      </Item>
      <Item>
           <FieldName name="fieldA">test4</FieldName>
           <FieldName name="fieldB">test5</FieldName>
           <FieldName name="fieldC">test6</FieldName>
      </Item>
</Items>

我正在使用以下 XSLT,但无法弄清楚如何从 field1、field2 和 field3 中选择值:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="mappingLookupDoc" select="document('lookup.xml')/lookup/fieldmapping "/>

<xsl:key name="mappingKey" match="fieldmapping " use="@name"/>

<xsl:template match="report">
    <xsl:apply-templates select="$mappingLookupDoc"/>
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="/">
    <Items xmlns="http://www.w3.org/1999/xhtml" schemaVersion="1.0">
        <xsl:for-each select="report/row">
            <Item>
                <xsl:for-each select="$mappingLookupDoc">
                    <xsl:variable name="fieldname" select="@name"/>
                    <FieldName>
                    <xsl:attribute name="name">
                            <xsl:value-of select="." />
                    </xsl:attribute>
                    <xsl:value-of select="/report/row/?????"/>
                    </FieldName>
                </xsl:for-each>
            </Item>
        </xsl:for-each>
    </Items>
</xsl:template>

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:

<Report>
      <row>
            <field1>test1</field1>
            <field2>test2</field2>
            <field3>test3</field3>
      </row>
      <row>
            <field1>test4</field1>
            <field2>test5</field2>
            <field3>test6</field3>
      </row>
</Report>

The lookup xml file looks like this:

<lookup>
      <fieldmapping name="field1">fieldA</fieldmapping>
      <fieldmapping name="field2">fieldB</fieldmapping>
      <fieldmapping name="field3">fieldC</fieldmapping>
</lookup>

Here is the output xml I want:

<Items>
      <Item>
           <FieldName name="fieldA">test1</FieldName>
           <FieldName name="fieldB">test2</FieldName>
           <FieldName name="fieldC">test3</FieldName>
      </Item>
      <Item>
           <FieldName name="fieldA">test4</FieldName>
           <FieldName name="fieldB">test5</FieldName>
           <FieldName name="fieldC">test6</FieldName>
      </Item>
</Items>

I am using the following XSLT and can not figure out how to select the value from field1, field2, and field3:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="mappingLookupDoc" select="document('lookup.xml')/lookup/fieldmapping "/>

<xsl:key name="mappingKey" match="fieldmapping " use="@name"/>

<xsl:template match="report">
    <xsl:apply-templates select="$mappingLookupDoc"/>
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="/">
    <Items xmlns="http://www.w3.org/1999/xhtml" schemaVersion="1.0">
        <xsl:for-each select="report/row">
            <Item>
                <xsl:for-each select="$mappingLookupDoc">
                    <xsl:variable name="fieldname" select="@name"/>
                    <FieldName>
                    <xsl:attribute name="name">
                            <xsl:value-of select="." />
                    </xsl:attribute>
                    <xsl:value-of select="/report/row/?????"/>
                    </FieldName>
                </xsl:for-each>
            </Item>
        </xsl:for-each>
    </Items>
</xsl:template>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晨说晚安 2024-10-24 03:25:19

我会这样做(假设 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>

[编辑]
以下是上述 XSLT 2.0 样式表的 XSLT 1.0 重写:

<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>

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>
情丝乱 2024-10-24 03:25:19

这就是我的做法(尽管我喜欢 Martin 使用 xsl:key):

我修改了 main.xml 和 Lookup.xml 以显示动态名称查找。

ma​​in.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 样式表

<?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>

输出

<?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>

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>
轻拂→两袖风尘 2024-10-24 03:25:19

也许这样的事情会更简单:

<?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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文