需要xslt样式表从输入xml下面转换为输出xml

时间:2016-09-13 22:08:18

标签: xml xslt

需要比较的价值 info/targetAudienceInfo/labelLocaleTextIDinfo/languageTranslation/localeTextID,如果匹配,则需要在<label>标记下显示相应languageTranslation / localeText的值,而不是<xmlTag>

同样需要比较info/targetAudienceInfo/standAloneLabelLocaleTextIDinfo/languageTranslation/localeTextID的值,如果匹配则需要在languageTranslation/localeTex标记下显示相应<standAloneLabel> t的值,而不是{ {1}}使用xslt。

下面是输入xml:

<xmlTag>

以下是预期输出xml:

<info>
  <xmlTag>sender</xmlTag>
  <displayOrder>103</displayOrder>
  <targetAudienceInfo>
     <targetAudienceCode>SERVE</targetAudienceCode>
     <labelLocaleTextID>417</labelLocaleTextID>
     <standAloneLabelLocaleTextID>404</standAloneLabelLocaleTextID>
  </targetAudienceInfo>
  <languageTranslation>
     <localeTextID>417</localeTextID>
     <languageISOCode>ENGLISH</languageISOCode>
     <languageLocaleID>en-US</languageLocaleID>
     <localeText>Sender1</localeText>
  </languageTranslation>
  <languageTranslation>
     <localeTextID>404</localeTextID>
     <languageISOCode>ENGLISH</languageISOCode>
     <languageLocaleID>en-US</languageLocaleID>
     <localeText>Sender2</localeText>
  </languageTranslation>

1 个答案:

答案 0 :(得分:0)

以下样式表将在应用于您的输入示例时生成预期结果。你没有说如果没有匹配会发生什么,或者有不止一场比赛。

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="trans" match="languageTranslation" use="localeTextID" />

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

<xsl:template match="xmlTag">
    <label>
        <xsl:value-of select="key('trans', ../targetAudienceInfo/labelLocaleTextID)/localeText" />
    </label>
    <standAloneLabel>
        <xsl:value-of select="key('trans', ../targetAudienceInfo/standAloneLabelLocaleTextID)/localeText" />
    </standAloneLabel>
</xsl:template>

</xsl:stylesheet>