我想使用 java 传递属性值列表,并想检查它是否存在于特定节点的属性中。现在我需要写一个 xslt 来实现以下,请提供一些指导。
<!-- 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>
我将使用以下方法设置参数,其中参数值是数组/数组列表
<!-- 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,第二个是预期的输出。传递的参数值是"属性"元素的"键"属性的属性值列表。
为简单起见,我对传递参数的格式进行了调整。如果无法做到这一点,则需要添加命名模板(或使用 EXSLT 函数,如果您的处理器支持),以便标记参数。或者考虑将此列表放在单独的 XML 文档中。
XSLT 1.0 (+ EXSLT node-set() 函数)
<?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>