【问题标题】:XSLT how to create one string with more elements in hierarchyXSLT如何在层次结构中创建一个具有更多元素的字符串
【发布时间】:2018-02-09 15:04:49
【问题描述】:

我有这个 XML 源:

<HEADER>
      <ODL_CODE>589146586</ODL_CODE>
      <ADR_CODE>154116515</ADR_CODE>
      <MNV_HER>EUR</MNV_HER>
      <IKS>
         <TYPE_CU>BY</TYPE_CU>
         <ID_CU>549745</ID_CU>
         <NAME>Company name</NAME>
         <ADDRESS>Street 123, City</ADDRESS>
         <TEL>+45 789 632 548</TEL>
         <MAIL>mail@address.com</MAIL>
      </IKS>
      <IKS>
         <TYPE_CU>IV</TYPE_CU>
         <ID_CU>7894156</ID_CU>
         <NAME>Company name #2</NAME>
         <ADDRESS>Street 123, City #2</ADDRESS>
         <TEL>+45 789 632 548 #2</TEL>
         <MAIL>mail@address.com #2</MAIL>
      </IKS>
 </HEADER>

我想要这个输出:

 <HEADER>
      <ODL_CODE>589146586</ODL_CODE>
      <ADR_CODE>154116515</ADR_CODE>
      <MNV_HER>EUR</MNV_HER>
      <IKS>
         <TYPE_CU>BY</TYPE_CU>
         <ID_CU>549745</ID_CU>
         <NAME>Company name</NAME>
         <ADDRESS>Street 123, City</ADDRESS>
         <TEL>+45 789 632 548</TEL>
         <MAIL>mail@address.com</MAIL>
      </IKS>
      <IKS>
         <TYPE_CU>IV</TYPE_CU>
         <ID_CU>7894156</ID_CU>
         <NAME>Company name #2</NAME>
         <ADDRESS>Street 123, City #2</ADDRESS>
         <TEL>+45 789 632 548 #2</TEL>
         <MAIL>mail@address.com #2</MAIL>
      </IKS>
      <POZN_1>Name: Company name #2, Street: Street 123, City #2, TEL: +45 789 632 548 #2</POZN_1>
 </HEADER>

如何在 XSLT 中编写它?我在“选择值”中尝试连接函数,但是代码 TYPE_CU、ID_CU 等中的 2 个元素有问题。 一定是TYPE_CU中的“IV”标识,但不知道为什么。

【问题讨论】:

  • 您可以使用您尝试过的任何 XSLT 来编辑您的问题吗?另外,您能解释一下您要实现的逻辑吗?谢谢!
  • 我不明白问题的本质。是关于如何选择正确的元素来构建&lt;POZN_1&gt; 的值吗? concatenate() 和它有什么关系? (事实上​​,为什么你需要concatenate()?)

标签: xml xslt xslt-1.0 xslt-2.0


【解决方案1】:

您需要显示您尝试为连接值执行的代码:

使用此代码希望对您有所帮助:

<?xml version="1.0" encoding="UTF-8"?>
<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:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

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

    <xsl:template match="IKS[TYPE_CU='IV']">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
        <POZN_1><xsl:value-of select="concat('Name: ' , NAME, ', Street: ', ADDRESS, ', TEL: ', TEL)"/></POZN_1>
    </xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多