我正在尝试使用XSLT中的“key”函数来提取值并将其添加到 XML中的特定位置.但我没有把它做对.下面是输入和XSLT. 请帮忙. 输入XML: input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs
请帮忙.
输入XML:
<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation="nbA2.8.90.xsd">
<info>User Info </info>
<team>User Team </team>
<nodeA id="test">
<inodeAA> Sample </inodeAA>
<inodeAB> Samples </inodeAB>
</nodeA>
<nodeA id="test2">
<inodeAA> Sample </inodeAA>
<inodeAB> Samples </inodeAB>
</nodeA>
<ns3:output xmlns:ns3="http://mysample.org">
<ns3:reply id="test">
<ns3:pkey>55555</ns3:pkey>
<ns3:place>SampleLoc</ns3:place>
</ns3:reply>
<ns3:reply id="test2">
<ns3:pkey>55557</ns3:pkey>
<ns3:place>SampleLoc2</ns3:place>
</ns3:reply>
</ns3:output>
</input>
预期产出:
<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation="nbA2.8.90.xsd">
<info>User Info </info>
<team>User Team </team>
<nodeA id="test">
<inodeAA> Sample </inodeAA>
<pkey>55555</pkey>
<inodeAB> Samples </inodeAB>
</nodeA>
<nodeA id="test2">
<inodeAA> Sample </inodeAA>
<pkey>55557</pkey>
<inodeAB> Samples </inodeAB>
</nodeA>
</input>
鉴于以下是我的XSL:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ns3="http://mysample.org"
exclude-result-prefixes="soap xsl">
<xsl:output omit-xml-declaration="no" indent="yes" />
<xsl:key name="parKey" match="ns3:output/ns3:reply/ns3:pkey" use="/input/ns3:output/ns3:reply/@id"></xsl:key>
<xsl:template match="@*|node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/input">
<xsl:variable name="output_186" select="ns3:output"/>
<xsl:copy>
<xsl:copy-of copy-namespaces="no" select="./@*" />
<xsl:copy-of select=" * except $output_186"></xsl:copy-of>
</xsl:copy>
</xsl:template>
<xsl:template match="/input//nodeA">
<xsl:copy>
<xsl:copy-of copy-namespaces="no" select="./@*" />
<pkey>
<xsl:copy-of select="key('parKey', @id)|." />
</pkey>
<xsl:copy-of copy-namespaces="no" select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出如下:未显示我的必填字段
<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation="nbA2.8.90.xsd" >
<info>User Info </info>
<team>User Team </team>
<nodeA id="test">
<inodeAA> Sample </inodeAA>
<inodeAB> Samples </inodeAB>
</nodeA>
<nodeA id="test2">
<inodeAA> Sample </inodeAA>
<inodeAB> Samples </inodeAB>
</nodeA>
</input>
请尽我所能帮助我.
谢谢…
更短的解决方案:<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns3="http://mysample.org">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kpKeyByParentId" match="ns3:pkey" use="../@id"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="inodeAA">
<xsl:call-template name="identity"/>
<xsl:apply-templates select="key('kpKeyByParentId', ../@id)"/>
</xsl:template>
<xsl:template match="ns3:pkey">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="ns3:output"/>
</xsl:stylesheet>
在提供的XML文档上应用此转换时:
<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation="nbA2.8.90.xsd">
<info>User Info </info>
<team>User Team </team>
<nodeA id="test">
<inodeAA> Sample </inodeAA>
<inodeAB> Samples </inodeAB>
</nodeA>
<nodeA id="test2">
<inodeAA> Sample </inodeAA>
<inodeAB> Samples </inodeAB>
</nodeA>
<ns3:output xmlns:ns3="http://mysample.org">
<ns3:reply id="test">
<ns3:pkey>55555</ns3:pkey>
<ns3:place>SampleLoc</ns3:place>
</ns3:reply>
<ns3:reply id="test2">
<ns3:pkey>55557</ns3:pkey>
<ns3:place>SampleLoc2</ns3:place>
</ns3:reply>
</ns3:output>
</input>
产生了想要的正确结果:
<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="nbA2.8.90.xsd">
<info>User Info </info>
<team>User Team </team>
<nodeA id="test">
<inodeAA> Sample </inodeAA>
<pkey>55555</pkey>
<inodeAB> Samples </inodeAB>
</nodeA>
<nodeA id="test2">
<inodeAA> Sample </inodeAA>
<pkey>55557</pkey>
<inodeAB> Samples </inodeAB>
</nodeA>
</input>
说明:
>标识规则复制为其选择执行的每个节点“按原样”.>与inodeAA匹配的ovveriding模板调用身份模板复制自身,然后在任何ns3:pkey元素上应用模板,其父ID的id属性与此inodeAA父级的id属性具有相同的值.为了方便和有效,这是通过调用引用“kpKeyByParentId”键的key()函数来完成的,该键将ns3:pkey定义为其父项的id属性的函数.>与ns3:pkey元素匹配的模板会创建一个元素(在无命名空间中),其名称是匹配元素的本地名称,并且还复制其内容.>元素ns3:输出及其整个子树被具有空体的匹配模板从处理(“删除”)中排除.
