Функциональность объединения двух файлов XSL в один файл (продолжение моих предыдущих Q's…)

StackOverflow https://stackoverflow.com/questions/4669156

Вопрос

Это в продолжении моих предыдущих вопросов (извините за повторное выступление аналогичного типа вопроса):

Функциональность слияния двух файлов XSL в один файл (а не импорт XSL или включить проблему)

а также

Функциональность объединения двух файлов XSL в один файл (продолжение .....)

Это на самом деле немного манипулирование моим вторым вопросом. Теперь мне нужно объединить решение, предоставленное Флак К моему первому вопросу с условием «Выберите» в моем XSL:

<xsl:choose>
          <xsl:when test='/Declaration/Header/DeclarantsReference = ""'>
            <DeclarantsReference>
              <xsl:text disable-output-escaping="no">A</xsl:text>
            </DeclarantsReference>
          </xsl:when>
          <xsl:otherwise>
            <DeclarantsReference>
              <xsl:value-of select="/Declaration/Header/DeclarantsReference"/>
            </DeclarantsReference>
          </xsl:otherwise>
        </xsl:choose>

Теперь любой образец ввода XML как:

    <Declaration>
         <Message>
            <Meduim>#+#</Meduim>
            <CommonAccessReference></CommonAccessReference>
         </Message>
         <BeginingOfMessage>
            <MessageCode>5</MessageCode>
            <DeclarationCurrency></DeclarationCurrency>
            <MessageFunction>ISD</MessageFunction>
         </BeginingOfMessage>
         <Header>
            <DeclarantsReference></DeclarantsReference>
            <Items>
            <Documents>
                  <ItemDocument>
                     <DocumentCode>XXX</DocumentCode>
                     <DocumentPart></DocumentPart>
                     <DocumentLanguage>#+#</DocumentLanguage>
                  </ItemDocument>
               </Documents>
            </Items>
           </Header>
</Declaration>

должен вывести:

<Declaration>
 <Message>
  <Meduim></Meduim>
 </Message>
 <BeginingOfMessage>
  <MessageCode>5</MessageCode>
  <MessageFunction>ISD</MessageFunction>
 </BeginingOfMessage>
 <Header>
 <DeclarantsReference>A</DeclarantsReference>
  <Items>
   <Documents>
    <ItemDocument>
     <DocumentCode>XXX</DocumentCode>
     <DocumentLanguage></DocumentLanguage>
    </ItemDocument>
   </Documents>
  </Items>
 </Header>
</Declaration>

Спасибо за любую помощь заранее.

Это было полезно?

Решение

Эта таблица стилей:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*[not(node())]"/>
    <xsl:template match="text()" name="strip">
        <xsl:param name="pString" select="."/>
        <xsl:param name="pOutput" select="substring-before($pString,'#+#')"/>
        <xsl:choose>
            <xsl:when test="contains($pString,'#+#')">
                <xsl:call-template name="strip">
                    <xsl:with-param name="pString"
                                    select="substring-after($pString,'#+#')"/>
                    <xsl:with-param name="pOutput"
                                    select="concat($pOutput,
                                                   substring-before($pString,
                                                                    '#+#'))"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat($pOutput,$pString)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="DeclarantsReference[not(node())]"
                  priority="1">
        <xsl:copy>A</xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Выход:

<Declaration>
    <Message>
        <Meduim></Meduim>
    </Message>
    <BeginingOfMessage>
        <MessageCode>5</MessageCode>
        <MessageFunction>ISD</MessageFunction>
    </BeginingOfMessage>
    <Header>
        <DeclarantsReference>A</DeclarantsReference>
        <Items>
            <Documents>
                <ItemDocument>
                    <DocumentCode>XXX</DocumentCode>
                    <DocumentLanguage></DocumentLanguage>
                </ItemDocument>
            </Documents>
        </Items>
    </Header>
</Declaration>

Примечание: Правила перезаписывают правило идентичности.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top