2014-03-04 45 views
-1

我想傳遞一個使用java的屬性值列表,並希望檢查它是否存在於特定節點的屬性中。現在我需要寫一個xslt來實現以下內容,請提供一些指導。XSLT匹配使用Java傳遞的屬性列表

<!-- language: lang-xml --> 

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<mtxml> 
<Nav> 
<DimVal Name="G" Id="10000" Flags="0" dim="10000" DdmName="Gender" > 
    <Property Key="A">10047</Property> 
    <Property Key="B">10048</Property> 
    <Property Key="C">10049</Property> 
    <Property Key="D">2082</Property> 
    <Property Key="G">22332</Property> 
    <Property Key="H">1121</Property> 
    <Property Key="I">2223</Property> 
</DimVal> 
<DimVal Name="A" Id="11000" Flags="0" dim="11000" DdmName="Address" > 
    <Property Key="A">10047</Property> 
    <Property Key="B">10048</Property> 
    <Property Key="D">2082</Property> 
    <Property Key="E">23343</Property> 
    <Property Key="F">3323</Property> 
    <Property Key="G">22332</Property> 
    <Property Key="H">1121</Property> 
    <Property Key="I">2223</Property> 
</DimVal> 
<DimVal Name="R" Id="11001" Flags="0" dim="11001" DdmName="Zip" > 
    <Property Key="A">10047</Property> 
    <Property Key="B">10048</Property> 
    <Property Key="C">44532</Property> 
    <Property Key="D">2082</Property> 
    <Property Key="E">23343</Property> 
    <Property Key="F">3323</Property> 
    <Property Key="G">22332</Property> 
    <Property Key="H">1121</Property> 
    <Property Key="I">2223</Property> 
</DimVal> 
</Nav> 
<Nav> 
<DimVal Name="GA" Id="90000" Flags="0" dim="90000" DdmName="Age" > 
    <Property Key="A">10047</Property> 
    <Property Key="D">2082</Property> 
    <Property Key="G">22332</Property> 
    <Property Key="H">1121</Property> 
    <Property Key="I">2223</Property> 
</DimVal> 
<DimVal Name="AL" Id="91000" Flags="0" dim="91000" DdmName="Route" > 
    <Property Key="A">10047</Property> 
    <Property Key="B">10048</Property> 
    <Property Key="D">2082</Property> 
    <Property Key="E">23343</Property> 
    <Property Key="F">3323</Property> 
    <Property Key="G">22332</Property> 
    <Property Key="H">1121</Property> 
    <Property Key="I">2223</Property> 
</DimVal> 
<DimVal Name="RW" Id="91001" Flags="0" dim="91001" DdmName="Postal Code" > 
    <Property Key="A">10047</Property> 
    <Property Key="B">10048</Property> 
    <Property Key="C">44532</Property> 
    <Property Key="D">2082</Property> 
    <Property Key="F">3323</Property> 
    <Property Key="G">22332</Property> 
    <Property Key="H">1121</Property> 
</DimVal> 
</Nav> 
</mtxml> 

我會使用以下,其中的parameterValue是陣列/ ArrayList的

<!-- language: lang-java--> 
xsltTransformer.setParameter(parameterName, parameterValue); 

現在,如果我傳遞{ 'A', 'B', 'C', 'd' 可設置參數, 'E', 'F', 'G', 'H', 'I'}的我會期待XSLT輸出如下:

<!-- language: lang-xml --> 

<DimVal Name="G" Id="10000" Flags="0" dim="10000" DdmName="Gender"> 
    <Property Key="E" /> 
    <Property Key="F" /> 
</DimVal> 
<DimVal Name="A" Id="11000" Flags="0" dim="11000" DdmName="Address"> 
    <Property Key="C" /> 
</DimVal> 
<DimVal Name="GA" Id="90000" Flags="0" dim="90000" DdmName="Age" > 
    <Property Key="B" /> 
    <Property Key="C" /> 
    <Property Key="E" /> 
    <Property Key="F" /> 
</DimVal> 
<DimVal Name="AL" Id="91000" Flags="0" dim="91000" DdmName="Route" > 
    <Property Key="C" /> 
</DimVal> 
<DimVal Name="RW" Id="91001" Flags="0" dim="91001" DdmName="Postal Code" > 
    <Property Key="E" /> 
    <Property Key="I" /> 
</DimVal> 

我沒有包含任何XSLT(我做不到工作它),第一XML是輸入XML和第二是預期的輸出。傳遞的參數值是'Property'元素的'Key'屬性的屬性值列表。

+0

要閱讀XLST內部參數,你只需要使用一個頂級'的'。你的'parameterValue'是字符串'「{'A','B','C','D','E','F','G','H','I'}」'?目前還不清楚您是否包含源文檔。這兩份文件都是預期結果嗎如何在與結果XML相關的parameterValue中發送字母? – helderdarocha

+0

我沒有合併任何xslt,第一個XML是輸入xml,第二個是預期的輸出。傳遞的參數值是'Property'元素的'Key'屬性的屬性值列表。 – Tirtha

+0

如果您沒有合併任何XSLT,那麼您希望如何從'xsltTransformer'獲得任何輸出? – Tomalak

回答

1

爲了簡單起見,我對傳遞參數的格式進行了調整。如果這不可行,您需要添加一個命名模板(或者如果您的處理器支持它,則使用EXSLT函數)以標記參數。或者考慮將這個列表放在單獨的XML文檔中。

XSLT 1.0(+ EXSLT節點集()函數)

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:exsl="http://exslt.org/common" 
extension-element-prefixes="exsl"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:param name="required-keys"> 
    <key>A</key> 
    <key>B</key> 
    <key>C</key> 
    <key>D</key> 
    <key>E</key> 
    <key>F</key> 
    <key>G</key> 
    <key>H</key> 
    <key>I</key> 
</xsl:param> 

<xsl:variable name="required-keys-set" select="exsl:node-set($required-keys)" /> 
<xsl:variable name="required-count" select="count($required-keys-set/key)" /> 

<xsl:template match="/"> 
<output> 
    <xsl:for-each select="mtxml/Nav/DimVal[not(count(Property/@Key[.=$required-keys-set/key])=$required-count)]"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:variable name="my-keys" select="Property/@Key"/> 
      <xsl:for-each select="$required-keys-set/key[not(.=$my-keys)]"> 
       <Property Key="{.}"/> 
      </xsl:for-each> 
     </xsl:copy> 
    </xsl:for-each> 
</output> 
</xsl:template> 

</xsl:stylesheet> 

當施加到您的輸入(減去在文檔的頂部的非法評論),其結果是:

<?xml version="1.0" encoding="UTF-8"?> 
<output> 
    <DimVal Name="G" Id="10000" Flags="0" dim="10000" DdmName="Gender"> 
     <Property Key="E"/> 
     <Property Key="F"/> 
    </DimVal> 
    <DimVal Name="A" Id="11000" Flags="0" dim="11000" DdmName="Address"> 
     <Property Key="C"/> 
    </DimVal> 
    <DimVal Name="GA" Id="90000" Flags="0" dim="90000" DdmName="Age"> 
     <Property Key="B"/> 
     <Property Key="C"/> 
     <Property Key="E"/> 
     <Property Key="F"/> 
    </DimVal> 
    <DimVal Name="AL" Id="91000" Flags="0" dim="91000" DdmName="Route"> 
     <Property Key="C"/> 
    </DimVal> 
    <DimVal Name="RW" Id="91001" Flags="0" dim="91001" DdmName="Postal Code"> 
     <Property Key="E"/> 
     <Property Key="I"/> 
    </DimVal> 
</output> 

請注意所需輸出中缺少的添加根元素。

傳遞的參數更改爲:

<key>A</key> 
<key>F</key> 

會產生以下結果:

<?xml version="1.0" encoding="UTF-8"?> 
<output> 
    <DimVal Name="G" Id="10000" Flags="0" dim="10000" DdmName="Gender"> 
     <Property Key="F"/> 
    </DimVal> 
    <DimVal Name="GA" Id="90000" Flags="0" dim="90000" DdmName="Age"> 
     <Property Key="F"/> 
    </DimVal> 
</output>