简体   繁体   English

使用xslt将后续元素作为xml中的属性

[英]subsequent element as attribute in xml using xslt

need help in making subsequent element as attribute to preceding element if subsequent element contains the preceding element name using XSLT. 如果后续元素包含使用XSLT的先前元素名称,则需要帮助使后续元素成为先前元素的属性。

For below example, < emp _id> contains the preceding the element name so need to convert this element as attribute to element. 对于下面的示例,< emp _id>包含前面的元素名称,因此需要将此元素作为属性转换为element。 can you anyone help for this?. 有人可以帮忙吗? I tried using subsequent functions in xslt but not working. 我尝试在xslt中使用后续功能,但无法正常工作。 Thanks in advance. 提前致谢。

Sample XML: 样本XML:

<root>
    <emp>test</emp>
    <emp_id>1234</emp_id>
    <college>something</college>
</root>

Expected out: 预期输出:

<root>
    <emp id="1234">test</emp>
    <college>something</college>
</root>

In the given example, you could do: 在给定的示例中,您可以执行以下操作:

XSLT 1.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:template match="*">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="following-sibling::*[starts-with(name(), name(current()))]" mode="attr"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="attr">
    <xsl:attribute name="{substring-after(name(), '_')}">
        <xsl:value-of select="." />
    </xsl:attribute>
</xsl:template>

<xsl:template match="*[preceding-sibling::*[name()=substring-before(name(current()), '_')]]"/>

</xsl:stylesheet>

to achieve the expected result. 达到预期的效果。


There is probably a more elegant way, but we need to have some more rules , not just a single example. 也许有一种更优雅的方法,但是我们需要更多的规则 ,而不仅仅是一个示例。


Added: 添加:

Assuming that every element whose name contains a _ has a "parent" element whose attribute it should become, you can start by applying templates to only the "parent" elements (ie elements whose name does not contain a '_'). 假设每个名称中包含_元素都有一个“父”元素,其属性_为“父”,您可以首先将模板应用于“父”元素(即,其名称中不包含“ _”的元素)。

Then use a key to collect the "child" elements that need to be converted to attributes. 然后使用一个来收集需要转换为属性的“子”元素。

XSLT 1.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="atr" match="*" use="substring-before(name(), '_')" />

<xsl:template match="/root">
    <xsl:copy>
        <xsl:apply-templates select="*[not(contains(name(), '_'))]"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:for-each select="key('atr', name())">
            <xsl:attribute name="{substring-after(name(), '_')}">
                <xsl:value-of select="." />
            </xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM