XSLT:将单个元素的属性转换为新元素

时间:2018-05-30 15:13:42

标签: xml xslt

基本上我想只获取一个特定元素(category)的属性,并创建一个新的子元素(categoryid),其属性值转换为元素值。

我想转换这个......

<root>
  <category id="123">
    <something id="777">987</something>
    <nothing>555</nothing>
  </category>
</root>

到此......

<root>
  <category>
    <categoryid>123</categoryid>
    <something id="777">987</something>
    <nothing>555</nothing>
  </category>
</root>

2 个答案:

答案 0 :(得分:3)

identity transform开始,添加模板以匹配您要转换为元素的属性...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="category/@id">
    <categoryid>
      <xsl:value-of select="."/>
    </categoryid>
  </xsl:template>

</xsl:stylesheet>

小提琴:http://xsltfiddle.liberty-development.net/eiZQaFa

答案 1 :(得分:0)

    public static boolean checkDiagnol(String[][] board, int counter, String playerMoving, int lastPlacedTileRow, int col) {

    for (int i = lastPlacedTileRow-1; q = col-1; i >= 0, q >=0; i--,q--){
            if (board[i][q] == playerMoving) {
                counter += 1;
            } else {
                break;
            }
            if (counter > 4) {
                return true;
            }
    }
    for (int i = lastPlacedTileRow + 1, q = col +1; i < board.length, q < board[0].length; i++,q++) {

            if (board[i][q] == playerMoving) {
                counter += 1;
            } else {
                break;
            }
            if (counter > 4) {
                return true;
          } 
    }
    return false;

}