简体   繁体   English

XSLT xsl:param 循环 - 如果在文本字符串中找到返回值

[英]XSLT xsl:param looping - Return value if found within a text string

Objective : Evaluate each <Report_Entry> element.目标:评估每个<Report_Entry>元素。 Return the <Internal_ID> value if the <External_ID> value is contained anywhere within the <Addenda> text.如果<External_ID>值包含在<Addenda>文本中的任何位置,则返回<Internal_ID>值。

My current XSLT returns the desired output but I am looking for a more dynamic/scalable solution.我当前的 XSLT 返回所需的 output 但我正在寻找更动态/可扩展的解决方案。 My XSLT requires that I create 2 xsl:param statements for each <Integration_Map_Value> :我的 XSLT 要求我为每个<Integration_Map_Value>创建 2 个xsl:param语句:

<xsl:param name="INT-MAP_External_ID_1" select="//Integration_Map_Value[1]/External_ID"/>

<xsl:param name="INT-MAP_Internal_ID_1" select="//Integration_Map_Value[1]/Internal_ID"/>

plus an <xsl:when> statement:加上一个<xsl:when>语句:

<xsl:when test="contains(upper-case(Addenda), $INT-MAP_External_ID_1)">
   <Internal_ID>
      <xsl:value-of select="$INT-MAP_Internal_ID_1"/>
   </Internal_ID>
</xsl:when>

Issue: Theoretically there could be an unlimited number of <Integration_Map_Value> instances so I am looking for a way of dynamically looping through all the <External_ID> values and comparing them against the <Addenda> field without having to create an endless amount of parameters, hardcoded instance numbers (ie [1]. [2], [3], etc.), and <xsl:when> statements.问题:理论上可能存在无限数量的<Integration_Map_Value>实例,因此我正在寻找一种动态循环所有<External_ID>值并将它们与<Addenda>字段进行比较的方法,而无需创建无穷无尽的参数,硬编码的实例编号(即 [1]. [2]、[3] 等)和<xsl:when>语句。

Source XML:来源 XML:

<root>
    <Integration_Map_Data>
        <Integration_Map_Value>
            <External_ID>MICROSOFT 1234</External_ID>
            <Internal_ID>XYZ-001</Internal_ID>
        </Integration_Map_Value>
        <Integration_Map_Value>
            <External_ID>MEGACORP</External_ID>
            <Internal_ID>C10025</Internal_ID>
        </Integration_Map_Value>
        <Integration_Map_Value>
            <External_ID>WIDGET TOOLS, INC ABC</External_ID>
            <Internal_ID>C-000338</Internal_ID>
        </Integration_Map_Value>
    </Integration_Map_Data>
    <Report_Data>
        <Report_Entry>
            <Addenda>abc microsoft 1234567</Addenda>
            <OrderNo>4444</OrderNo>
        </Report_Entry>
        <Report_Entry>
            <Addenda>fubar MEGACORP LLC</Addenda>
            <OrderNo>5555</OrderNo>
        </Report_Entry>
        <Report_Entry>
            <Addenda>no expected match here</Addenda>
            <OrderNo>6666</OrderNo>
        </Report_Entry>
    </Report_Data>
</root>

Current XSLT:当前 XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

    <!-- External ID Parameters -->
    <xsl:param name="INT-MAP_External_ID_1" select="//Integration_Map_Value[1]/External_ID"/>
    <xsl:param name="INT-MAP_External_ID_2" select="//Integration_Map_Value[2]/External_ID"/>
    <xsl:param name="INT-MAP_External_ID_3" select="//Integration_Map_Value[3]/External_ID"/>

    <!-- Internal ID Parameters -->
    <xsl:param name="INT-MAP_Internal_ID_1" select="//Integration_Map_Value[1]/Internal_ID"/>
    <xsl:param name="INT-MAP_Internal_ID_2" select="//Integration_Map_Value[2]/Internal_ID"/>
    <xsl:param name="INT-MAP_Internal_ID_3" select="//Integration_Map_Value[3]/Internal_ID"/>

    <xsl:template match="/">
        <root>
            <xsl:for-each select="root/Report_Data/Report_Entry">
                <record>
                    <xsl:copy-of select="Addenda"/>
                    <xsl:copy-of select="OrderNo"/>

                    <xsl:choose>
                        <xsl:when test="contains(upper-case(Addenda), $INT-MAP_External_ID_1)">
                            <Internal_ID>
                                <xsl:value-of select="$INT-MAP_Internal_ID_1"/>
                            </Internal_ID>
                        </xsl:when>
                        <xsl:when test="contains(upper-case(Addenda), $INT-MAP_External_ID_2)">
                            <Internal_ID>
                                <xsl:value-of select="$INT-MAP_Internal_ID_2"/>
                            </Internal_ID>
                        </xsl:when>
                        <xsl:when test="contains(upper-case(Addenda), $INT-MAP_External_ID_3)">
                            <Internal_ID>
                                <xsl:value-of select="$INT-MAP_Internal_ID_3"/>
                            </Internal_ID>
                        </xsl:when>
                    </xsl:choose>
                </record>
            </xsl:for-each>
        </root>
    </xsl:template>
</xsl:stylesheet>

XML Output: XML Output:

<?xml version="1.0" encoding="utf-8"?>
<root>
   <record>
      <Addenda>abc microsoft 1234567</Addenda>
      <OrderNo>4444</OrderNo>
      <Internal_ID>XYZ-001</Internal_ID>
   </record>
   <record>
      <Addenda>fubar MEGACORP LLC</Addenda>
      <OrderNo>5555</OrderNo>
      <Internal_ID>C10025</Internal_ID>
   </record>
   <record>
      <Addenda>no expected match here</Addenda>
      <OrderNo>6666</OrderNo>
   </record>
</root>

I've exhausted my beginner XSLT skills and can't seem to figure out a more elegant solution to my problem.我已经用尽了我的初学者 XSLT 技能,似乎无法找到更优雅的解决方案来解决我的问题。 Any help is greatly appreciated.任何帮助是极大的赞赏。 I am open to using XSLT 3.0 but that is very new to me.我愿意使用 XSLT 3.0,但这对我来说很新。

Apologies for the title of this post... struggled to describe my issue.为这篇文章的标题道歉......很难描述我的问题。

Use a simple reference to the particular matching element eg使用对特定匹配元素的简单引用,例如

  <xsl:template match="Report_Entry">
    <record>
      <xsl:apply-templates select="node(), //Integration_Map_Value[contains(upper-case(current()/Addenda), External_ID)]/Internal_ID"/>
    </record>
  </xsl:template>

plus the identity transformation template plus加上身份转换模板加上

  <xsl:template match="root">
    <xsl:copy>
      <xsl:apply-templates select="Report_Data/Report_Entry"/>
    </xsl:copy>
  </xsl:template>

The identity transformation can be declared in XSLT 3 through <xsl:mode on-no-match="shallow-copy"/> , in earlier versions you have to spell it out as身份转换可以通过<xsl:mode on-no-match="shallow-copy"/>在 XSLT 3 中声明,在早期版本中,您必须将其拼写为

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM