【问题标题】:when replace the xml elements with the help of xsl stylesheet using java,not getting replaced在使用 java 的 xsl 样式表的帮助下替换 xml 元素时,不会被替换
【发布时间】:2017-01-17 17:57:34
【问题描述】:

如何用 xsl 替换子标签名称。 下面是我的xml结构。

  <Checkpax xmlns="http://xml.api.com/test">
    <customerLevel>
        <customerDetails>
            <paxDetails>
                <surname>MUKHERJEE</surname>
                <type>A</type>
                <gender>M</gender>
            </paxDetails>
            <otherPaxDetails>
                <givenName>JOY</givenName>
                <title>MR</title>
                <age>11</age>
            </otherPaxDetails>
            <otherPaxDetails>
                <title>MR</title>
            </otherPaxDetails>
        </customerDetails>
        <staffDetails>
            <staffInfo/>
            <staffCategoryInfo>
                <attributeDetails>
                    <attributeType>NA</attributeType>
                </attributeDetails>
            </staffCategoryInfo>
        </staffDetails>
        <productLevel>
            <legLevel>
                <legLevelIndicator>
                    <statusDetails>
                        <indicator>abc</indicator>
                        <action>1</action>
                    </statusDetails>
                </legLevelIndicator>
            </legLevel>
        </productLevel>
        <CustomerLevel>
            <legLevel>
                <legLevelIndicator>
                    <statusDetails>
                        <indicator>cde</indicator>
                        <action>1</action>
                    </statusDetails>
                </legLevelIndicator>
            </legLevel>
        </CustomerLevel>
    </customerLevel>
</Checkpax>

以下是我的 XSL 文件

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="customerLevel/productLevel/legLevel/legLevelIndicator/statusDetails">
        <statusInformation>
            <xsl:apply-templates select="@*|node()" />
        </statusInformation>
    </xsl:template>
</xsl:stylesheet>

此处的 statusDetails 名称应更改为 ProductLevel/LeglevelIndicator 中的 staffInformation。请给我这样做的建议。

以下是预期结果

<Checkpax xmlns="http://xml.api.com/test">
        <customerLevel>
            <customerDetails>
                <paxDetails>
                    <surname>MUKHERJEE</surname>
                    <type>A</type>
                    <gender>M</gender>
                </paxDetails>
                <otherPaxDetails>
                    <givenName>JOY</givenName>
                    <title>MR</title>
                    <age>11</age>
                </otherPaxDetails>
                <otherPaxDetails>
                    <title>MR</title>
                </otherPaxDetails>
            </customerDetails>
            <staffDetails>
                <staffInfo/>
                <staffCategoryInfo>
                    <attributeDetails>
                        <attributeType>NA</attributeType>
                    </attributeDetails>
                </staffCategoryInfo>
            </staffDetails>
            <productLevel>
                <legLevel>
                    <legLevelIndicator>
                        <statusInformation>
                            <indicator>abc</indicator>
                            <action>1</action>
                        </statusInformation>
                    </legLevelIndicator>
                </legLevel>
            </productLevel>
            <CustomerLevel>
                <legLevel>
                    <legLevelIndicator>
                        <statusDetails>
                            <indicator>cde</indicator>
                            <action>1</action>
                        </statusDetails>
                    </legLevelIndicator>
                </legLevel>
            </CustomerLevel>
        </customerLevel>
    </Checkpax>

【问题讨论】:

  • 请发布您希望得到的准确结果。
  • 现在更新了问题。

标签: java xml xslt stylesheet


【解决方案1】:

statusDetails的名字应该改成staffInformation里面 ProductLevel/LeglevelIndicator

试试这个方法:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://xml.api.com/test"
exclude-result-prefixes="ns0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="ns0:productLevel/ns0:legLevel/ns0:legLevelIndicator/ns0:statusDetails">
    <staffInformation xmlns="http://xml.api.com/test">
        <xsl:apply-templates/>
    </staffInformation>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2021-09-26
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    相关资源
    最近更新 更多