在XSLT中将值标记为元素名称

时间:2016-06-06 15:07:49

标签: xslt

我有一个xml:

<?xml version="1.0" encoding="UTF-16"?><SiebelMessage
 MessageId="1-2WPBF"
 IntObjectName="LHC Rules Items IO"
 MessageType="Integration Object"
 IntObjectFormat="Siebel Hierarchical"
><ListOfLhcRulesItemsBo
><LhcRuleItems
><Comments
>Pick@Unique</Comments
><Level
>3</Level
><Id
>1-2BCN3B</Id
><Branch
>Civil</Branch
><Category
>C.M. (Civil)</Category
><Rule
>Bench Type</Rule
><SubCategory
>Pauper</SubCategory
><Value
>Divisional</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Unique</Comments
><Level
>3</Level
><Id
>1-2BFEH7</Id
><Branch
>Civil</Branch
><Category
>C.M. (Civil)</Category
><Rule
>Court Fee</Rule
><SubCategory
>Pauper</SubCategory
><Value
>1</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Ignore</Comments
><Level
>2</Level
><Id
>1-2BIJY8</Id
><Branch
>Civil</Branch
><Category
>C.M. (Civil)</Category
><Rule
>Bench Type</Rule
><SubCategory
></SubCategory
><Value
>Single</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Ignore</Comments
><Level
>2</Level
><Id
>1-2BIJY9</Id
><Branch
>Civil</Branch
><Category
>C.M. (Civil)</Category
><Rule
>Court Fee</Rule
><SubCategory
></SubCategory
><Value
>200</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Ignore</Comments
><Level
>1</Level
><Id
>1-2BIRER</Id
><Branch
>Civil</Branch
><Category
></Category
><Rule
>Bench Type</Rule
><SubCategory
></SubCategory
><Value
>Single</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Ignore</Comments
><Level
>1</Level
><Id
>1-2BIRET</Id
><Branch
>Civil</Branch
><Category
></Category
><Rule
>Court Fee</Rule
><SubCategory
></SubCategory
><Value
>100</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Unique</Comments
><Level
>2</Level
><Id
>1-2BISGB</Id
><Branch
>Civil</Branch
><Category
>C.M. (Civil)</Category
><Rule
>Adjournment Duration</Rule
><SubCategory
></SubCategory
><Value
>2</Value
></LhcRuleItems
><LhcRuleItems
><Comments
></Comments
><Level
>1</Level
><Id
>1-2BS0UV</Id
><Branch
>Civil</Branch
><Category
></Category
><Rule
>Bench Rules</Rule
><SubCategory
></SubCategory
><Value
>BR</Value
></LhcRuleItems
></ListOfLhcRulesItemsBo
></SiebelMessage
>

我希望输出如下:

<Rules>
<BenchType>Divisional</BenchType>
<CourtFee>1</CourtFee>
</Rules>

我正在发布此帖Link中发布的解决方案,但它无效。我只获得值的输出。 当前输出:

<?xml version="1.0" encoding="UTF-8"?>
<SiebelMessage MessageId="1-2WPBG" IntObjectName="LHC Rules Items IO" MessageType="Integration Object" IntObjectFormat="Siebel Hierarchical">
<ListOfLhcRulesItemsBo>




</ListOfLhcRulesItemsBo>
</SiebelMessage>

尝试过XSLT:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
    <xsl:key name="UniqueRecords" match="LhcRuleItems" use="Rule"/>
    <xsl:template match="node()|@*" name="FinalRules">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="LhcRuleItems"/>
<xsl:template match="SiebelMessage/ListOfLhcRulesItemsBo/LhcRuleItems">
        <xsl:for-each select="Rule">
            <xsl:element name="{translate(Rule, ' ' , '')}">
                <xsl:value-of select="Value"/>
            </xsl:element>
        </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

使用完整的示例xml文件更新了问题。我更新了当前的XSLT,现在我没有输出任何内容。

1 个答案:

答案 0 :(得分:2)

您尝试无法工作的原因是您尝试将“Bench Type”和“Court Fee”设置为元素名称 - 但元素名称不能包含空格。

如果你改变:

<xsl:element name="{Rule}">

为:

<xsl:element name="{translate(Rule, ' ' , '')}">

您将获得结果,应用于显示的示例。由于不同的原因,其他值可能不是有效的元素名称。

另请注意:

<xsl:value-of select="Rule"/>

应该是:

<xsl:value-of select="Value"/>

加了:

在您的新尝试中,您需要更改:

<xsl:template match="SiebelMessage/ListOfLhcRulesItemsBo/LhcRuleItems">
        <xsl:for-each select="Rule">
            <xsl:element name="{translate(Rule, ' ' , '')}">
                <xsl:value-of select="Value"/>
            </xsl:element>
        </xsl:for-each>
</xsl:template>

为:

<xsl:template match="SiebelMessage/ListOfLhcRulesItemsBo/LhcRuleItems">
    <xsl:element name="{translate(Rule, ' ' , '')}">
        <xsl:value-of select="Value"/>
    </xsl:element>
</xsl:template>

即。删除您添加的xsl:for-each指令 - 原始问题中没有。

无论如何你有太多的代码。您已经定义了一个根本没有使用的密钥,并且您有两个与相同LhcRuleItems元素匹配的冲突模板。获取指定输出所需要做的就是:

<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:template match="/SiebelMessage">
    <Rules>
        <xsl:for-each select="ListOfLhcRulesItemsBo/LhcRuleItems">
            <xsl:element name="{translate(Rule, ' ' , '')}">
                <xsl:value-of select="Value"/>
            </xsl:element>
        </xsl:for-each>  
    </Rules>
</xsl:template>

</xsl:stylesheet>