gpt4 book ai didi

xml - 使用 key 函数将元素添加到 XML 无法使用 xslt

转载 作者:数据小太阳 更新时间:2023-10-29 02:08:48 26 4
gpt4 key购买 nike

我正在尝试使用 XSLT 中的“键”函数来提取值并将其添加到 XML 中的特定位置。但我没有做对。下面是输入和 XSLT。

请帮忙。

输入 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>

解释:

  1. 身份规则“按原样”复制每个选择执行的节点。

  2. inodeAA 匹配的验证模板调用身份模板复制自身,然后将模板应用于任何 ns3:pkey 元素,其父级的 id 属性与此 inodeAA 父级的 id 属性具有相同的值。为了方便和高效,这是通过调用引用 "kpKeyByParentId" 键的 key() 函数来完成的,该键将 ns3:pkey 定义为其父级的 id 属性的函数。

  3. 匹配 ns3:pkey 元素的模板创建一个元素(在无命名空间中),其名称是匹配元素的本地名称,并复制其内容。

  4. 元素 ns3:output 及其整个子树被具有空主体的匹配模板排除在处理之外(“删除”)。

关于xml - 使用 key 函数将元素添加到 XML 无法使用 xslt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13881403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com