3

私はxsltを学習しようとしていますが、次のようなxmlを使用しています。

<Beats>
 <Beat>
   <Personal type="set">
      <Usages type="box">
       1233
      </Usages>
      <NonUsages type="box">
       4122
      </NonUsages>
   </Personal>

   <NonPersonal type="unset">
      <Damages type="box">
       6466
      </Damages>
      <NonDamages type="box">
       5544
      </NonUsages>
   </NonPersonal>

   <Confidential type="set">
      <Discounts type="box">
       1233
      </Discounts>
      <NonDiscounts type="box">
       4122
      </NonDiscounts>
   </Confidential>

 </Beat>
</Beats>

私の現在の目的は、内側のタグ内の数字を印刷することです。ただし、属性'type'のみが重要であるため、タグの名前をセレクターとして使用することはできません。次のxsltを使用してみました。しかし、それはうまくいかなかったようです。

<xsl:output method="text"/>
<xsl:template match="*">
<html>
<body>
<h2> Test</h2>
<xsl:for-each select="//Beats/Beat/[@type='set']">
   <xsl:value-of select="[@type='box'] />
   <br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

私は何が間違っているのですか?

そして、私が理解できなかったもう1つのことは、属性をセレクターとして使用しながらタグの名前を取得する方法でした。それ以外の

<xsl:value-of select="[@type='box'] />

タグの中に何が入っているかを示します。この「type=box」属性を含むタグの名前を取得するために何を使用できますか?(たとえば、Usage、NonUsagesなど)

4

1 に答える 1

4

This transformation is intended to only provide the necessary corrections to your original transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <html>
            <body>
                <h2> Test</h2>
                <xsl:for-each select="/*/*/*[@type='set']/*[@type='box']">
                    <xsl:value-of select="." />
                    <br/>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document (after correcting it to make it well-formed XML document):

<Beats>
 <Beat>
   <Personal type="set">
      <Usages type="box">
       1233
      </Usages>
      <NonUsages type="box">
       4122
      </NonUsages>
   </Personal>

   <NonPersonal type="unset">
      <Damages type="box">
       6466
      </Damages>
      <NonDamages type="box">
       5544
      </NonDamages>
   </NonPersonal>

   <Confidential type="set">
      <Discounts type="box">
       1233
      </Discounts>
      <NonDiscounts type="box">
       4122
      </NonDiscounts>
   </Confidential>
 </Beat>
</Beats>

the (what I guess is) wanted result is produced:

<html>
   <body>
      <h2> Test</h2>
       1233
      <br/>
       4122
      <br/>
       1233
      <br/>
       4122
      <br/>
   </body>
</html>

and it is displayed in the browser as:

Test

1233
4122
1233
4122


On your second question:

And one more thing i couldnt figure out was how to get the name of tags while using attributes as selectors. Instead of

<xsl:value-of select="[@type='box'] />

You want something as:

<xsl:for-each select="/*/*/*[@type='set']/*[@type='box']">
  <xsl:value-of select="name()"/>
  <xsl:text> : </xsl:text>
  <xsl:value-of select="." />
  <br/>
</xsl:for-each>

The complete transformation now becomes:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <html>
            <body>
                <h2> Test</h2>
                <xsl:for-each select="/*/*/*[@type='set']/*[@type='box']">
                  <xsl:value-of select="name()"/>
                   <xsl:text> : </xsl:text>
                    <xsl:value-of select="." />
                    <br/>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

and when it is applied on the same XML document (above), the result is:

<html>
   <body>
      <h2> Test</h2>Usages : 
       1233
      <br/>NonUsages : 
       4122
      <br/>Discounts : 
       1233
      <br/>NonDiscounts : 
       4122
      <br/>
   </body>
</html>

and this is displayed by the browser as:

Test

Usages : 1233
NonUsages : 4122
Discounts : 1233
NonDiscounts : 4122

于 2012-10-19T22:56:17.293 に答える