需要XSLT示例

时间:2014-08-28 08:43:23

标签: xml xslt-1.0

我有一个xml需要用自定义标签创建xslt需要获得一些示例或者xslt上的任何帮助都会有所帮助。 这是输入xml我需要使用xslt

分成两个部分

输入:

<XMLJRN>
    <P>
        <E name="Statement_Document">Statement_Document</E>
        <E name="StatementIdentifier">MHS</E>
        <E name="CurrencyCode">USD</E>
        <E name="ClaimCount">2</E>

        <E name="Claim">Claim</E>
        <E name="ClaimNumber">050001741183100</E>
        <E name="AmountPaid">0.00</E>
        <E name="CurrencyCode">USD</E>

        <E name="Claim">Claim</E>
        <E name="ClaimNumber">050001741179800</E>
        <E name="AmountPaid">150.00</E>
        <E name="CurrencyCode">USD</E>
    </P>
</XMLJRN>

期望的输出:

<Doc1Statement_STG>
    <StatementDocument>Statement_Document</StatementDocument>
    <StatementIdentifier>MHS</StatementIdentifier>
    <ClaimCount>2</ClaimCount>
    <Doc1Claim_STG>
        <ClaimNumber>050001741183100</ClaimNumber>
        <AmountPaid>0.00</AmountPaid>
        <ClaimCurrencyCode>USD</ClaimCurrencyCode>
    </Doc1Claim_STG>
    <Doc1Claim_STG>
        <ClaimNumber>050001741179800</ClaimNumber>
        <AmountPaid>150.00</AmountPaid>
        <ClaimCurrencyCode>USD</ClaimCurrencyCode>
    </Doc1Claim_STG>
</Doc1Statement_STG>

我当前的XLST:

<xsl:template match="XMLJRN/P">
 <xsl:apply-templates/> 
</xsl:template>

<xsl:template match="E"> 
 <Doc1Statement_STG>
  <xsl:element name="{@name}"> 
  <xsl:value-of select="."/> 
  </xsl:element> 
 </Doc1Statement_STG>
</xsl:template>

我无法为每个索赔金额添加标记,任何帮助

1 个答案:

答案 0 :(得分:0)

假设输入中的每个声明都具有示例中显示的结构,您可以这样做:

XSLT 1.0

<?xml version="1.0" encoding="utf-8"?>
<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="/">
    <Doc1Statement_STG>
        <xsl:for-each select="XMLJRN/P">
            <StatementDocument>
                <xsl:value-of select="E[@name='Statement_Document']"/>
            </StatementDocument>
            <StatementIdentifier>
                <xsl:value-of select="E[@name='StatementIdentifier']"/>
            </StatementIdentifier>
            <ClaimCount>
                <xsl:value-of select="E[@name='ClaimCount']"/>
            </ClaimCount>
            <xsl:for-each select="E[@name='Claim']">
                <Doc1Claim_STG>
                    <ClaimNumber>
                        <xsl:value-of select="following-sibling::E[@name='ClaimNumber'][1]"/>
                    </ClaimNumber>
                    <AmountPaid>
                        <xsl:value-of select="following-sibling::E[@name='AmountPaid'][1]"/>
                    </AmountPaid>
                    <ClaimCurrencyCode>
                        <xsl:value-of select="following-sibling::E[@name='CurrencyCode'][1]"/>
                    </ClaimCurrencyCode>
                </Doc1Claim_STG>
            </xsl:for-each>
        </xsl:for-each>
    </Doc1Statement_STG>
</xsl:template>

</xsl:stylesheet>