1

私のxmlファイルの構造が次のようになっているとします。

 <?xml version="1.0" encoding="ISO-8859-1"?>
<main>
    <_All>
        <_999999>
            <Employee_Amount>10.0</Employee_Amount>
            <Cash_Count>10.0</Cash_Count>
            <Dine_in_Amount>10.0</Dine_in_Amount>
            <Promos_Actual>10.0</Promos_Actual>
            <Combo_Savings_Amount>10.0</Combo_Savings_Amount>
            <total_sales>10.0</total_sales>
            </_999999>
        <_test>
            <Employee_Amount>20.0</Employee_Amount>
            <Cash_Count>20.0</Cash_Count>
            <Dine_in_Amount>20.0</Dine_in_Amount>
            <Promos_Actual>20.0</Promos_Actual>
            <Combo_Savings_Amount>20.0</Combo_Savings_Amount>
            <total_sales>20.0</total_sales>
            </_test>
    </_All>
</main>

すべてのノードを合計する必要があります (たとえば、両方のノードの Cash_Count、両方のノードの total_sales)。ノード名は変更される可能性があるため、その名前を使用してナビゲートすることはできません。for-each を試しましたが、xpath 式に固執しました。私はxslt 1.0が初めてなので、同じことを手伝ってください。

プロスカーが回答で私に提案したものの助けを借りて、以下のxsltを試しました:

<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/*">
    <table border="1" cellspacing="1" cellpadding="1">
        <tr>
            <xsl:apply-templates select="/*/*/*" mode="wrap"/>
        </tr>
        <tr>
        <td><xsl:value-of select="name(child::*)"/></td>
        <td>&#160;</td>
        <xsl:for-each select="/*/*/*/*[current()]">
                          <xsl:variable name="pos" select="position()" />
                         <td> <xsl:value-of select="sum(/*/*/*/*[$pos])" /></td>
                  </xsl:for-each>
        </tr>
        <xsl:apply-templates select="/*/*/*" mode="values"/>
    </table>
  </xsl:template>

 <xsl:template match="/*/*/*" mode="wrap">
    <xsl:if test="position() = 1">
    <th>Region</th>
    <th>Store</th>
    <xsl:for-each select="./*">
    <th><xsl:value-of select="name()"/></th>
    </xsl:for-each>
    </xsl:if>
</xsl:template>
<xsl:template match="/*/*/*" mode="values">
    <xsl:for-each select=".">
        <tr>
            <td>&#160;</td>
            <td><xsl:value-of select="name()"/></td>
            <xsl:for-each select="./*">
            <td><xsl:value-of select="."/></td>
            </xsl:for-each>
        </tr>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

それはうまくいきましたが、値が0の余分なtdで出力を生成します。ノードが 6 つあるため、値が 0 の追加のtdが 6 つあります。合計後、このtrは次のようになります..

<tr>
<td>30</td>
<td>30</td>
<td>30</td>
<td>30</td>
<td>30</td>
<td>30</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>

誰でもそれを理解するのを手伝ってください。ありがとう

4

1 に答える 1

0

14 行目の xsl:for-each 要素の select-attribute を次のように変更します"/*[1]/*[1]/*[1]/*"

<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/*">
    <table border="1" cellspacing="1" cellpadding="1">
        <tr>
            <xsl:apply-templates select="/*/*/*" mode="wrap"/>
        </tr>
        <tr>
        <td><xsl:value-of select="name(child::*)"/></td>
        <td>&#160;</td>
        <xsl:for-each select="/*[1]/*[1]/*[1]/*">
                          <xsl:variable name="pos" select="position()" />
                         <td> <xsl:value-of select="sum(/*/*/*/*[$pos])" /></td>
                  </xsl:for-each>
        </tr>
        <xsl:apply-templates select="/*/*/*" mode="values"/>
    </table>
  </xsl:template>

 <xsl:template match="/*/*/*" mode="wrap">
    <xsl:if test="position() = 1">
    <th>Region</th>
    <th>Store</th>
    <xsl:for-each select="./*">
    <th><xsl:value-of select="name()"/></th>
    </xsl:for-each>
    </xsl:if>
</xsl:template>
<xsl:template match="/*/*/*" mode="values">
    <xsl:for-each select=".">
        <tr>
            <td>&#160;</td>
            <td><xsl:value-of select="name()"/></td>
            <xsl:for-each select="./*">
            <td><xsl:value-of select="."/></td>
            </xsl:for-each>
        </tr>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
于 2013-01-16T23:33:19.360 に答える