自定义排序:如果父节点包含子节点,我需要检查每个子节点属性名称,然后使用XSLT从该元素中提取文本

时间:2013-05-17 07:23:36

标签: xml xslt

我在下面提到了我的示例xml文件。   我想以我自己的订单格式显示value标记文本。     EX:首先我要显示Value AttributeID="13222"     Value AttributeID="113458"像这样,我需要根据AttributeId排列位置。

sample.xml中

<?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl"?>
    <Values>     
      <Value AttributeID="113458" AttributeName="0180 - Text - remainder">
        <Text>
          <style name="TextHead1">The current enterprise application landscape</style>
          <style name="TextStyle1">
            <character name="linebreak" />Introduction of the Title
            <style name="bold">
              The smallest font size
              <character name="linebreak" />
              for which kerning should be automatically adjusted.
              <character name="linebreak" />
              and some manual work done.
            </style>
            <style name="bold">
              Reference for this book<style name="superscript">1</style>
            </style>     
        </Text>
      </Value>
      <Value AttributeID="13335" AttributeName="0190 - Text - remainder">
        <Text>
          <style name="TextHead1">The Bussiness application landscape</style>
          <style name="TextStyle1">
            Introduction of the Title
            <style name="bold">
              The smallest font size for which kerning should be automatically adjusted and some manual work done.
            </style>
             <style name="SimpleTag target=&quot;http://www.example.com/financing /uk&quot;"><style name="URL_bold">ibm.com</style></style>
            <style name="italic">
              Reference for this book<style name="superscript">1</style>
            </style>
            <style name="bulletStyle1">none: No leader line</style>
            <style name="bulletStyle1">dot: Dotted leader line</style>
          </Text>
      </Value>   
    <Value AttributeID="13222" AttributeName="0200 - Text - remainder">
        <Text>
          <style name="TextHead1">the world’s largest florist and gift shop</style>
          <style name="TextStyle1">
            Introduction of the Title, It is also introducing environmental responsibility as a theme across all of its businesses,        
              The smallest font size for some manual work done.       
             <style name="SimpleTag target=&quot;http://www.example.com/financing /uk&quot;"><style name="URL_bold">ibm.com</style></style>
            <style name="italic">
              Reference for this book<style name="superscript">1</style>
            </style>
            <style name="bulletStyle1">environmental responsibility</style>
            <style name="bulletStyle1">world’s largest florist</style>
          </Text>
      </Value>     
    </Values>

1 个答案:

答案 0 :(得分:0)

以下是如何在XSLT中设置自定义排序的示例:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="Values">
        <values>
          <xsl:for-each select="Value">
            <xsl:sort data-type="number" order="ascending"
              select="(number(@AttributeID='13222') * 1)
          + (number(@AttributeID='113458') * 2)
          + (number(@AttributeID='13335') * 3) "/>
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </values>
      </xsl:template>
    </xsl:stylesheet>

INPUT XML ,因为您的XML格式不正确:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"?>
<Values>     
  <Value AttributeID="113458" AttributeName="0180 - Text - remainder">
    <Text>
      <style name="TextHead1">The current enterprise application landscape</style>
      <style name="TextStyle1">
            <character name="linebreak" />Introduction of the Title
            <style name="bold">
              The smallest font size
              <character name="linebreak" />
              for which kerning should be automatically adjusted.
              <character name="linebreak" />
              and some manual work done.
            </style>
            <style name="bold">
              Reference for this book<style name="superscript">1</style>
            </style>
        </style>
        </Text>
  </Value>
  <Value AttributeID="13335" AttributeName="0190 - Text - remainder">
    <Text>
      <style name="TextHead1">The Bussiness application landscape</style>
      <style name="TextStyle1">
            Introduction of the Title
            <style name="bold">
              The smallest font size for which kerning should be automatically adjusted and some manual work done.
            </style>
             <style name="SimpleTag target=&quot;http://www.example.com/financing /uk&quot;"><style name="URL_bold">ibm.com</style></style>
            <style name="italic">
              Reference for this book<style name="superscript">1</style>
            </style>
            <style name="bulletStyle1">none: No leader line</style>
            <style name="bulletStyle1">dot: Dotted leader line</style>
        </style>
          </Text>
  </Value>   
  <Value AttributeID="13222" AttributeName="0200 - Text - remainder">
    <Text>
      <style name="TextHead1">the world’s largest florist and gift shop</style>
      <style name="TextStyle1">
            Introduction of the Title, It is also introducing environmental responsibility as a theme across all of its businesses,        
              The smallest font size for some manual work done.       
             <style name="SimpleTag target=&quot;http://www.example.com/financing /uk&quot;"><style name="URL_bold">ibm.com</style></style>
            <style name="italic">
              Reference for this book<style name="superscript">1</style>
            </style>
            <style name="bulletStyle1">environmental responsibility</style>
            <style name="bulletStyle1">world’s largest florist</style>
        </style>
          </Text>
  </Value>     
</Values>

生成的输出:

<?xml version="1.0" encoding="utf-8"?><values><Value AttributeID="13222" AttributeName="0200 - Text - remainder">
    <Text>
      <style name="TextHead1">the world’s largest florist and gift shop</style>
      <style name="TextStyle1">
            Introduction of the Title, It is also introducing environmental responsibility as a theme across all of its businesses,        
              The smallest font size for some manual work done.       
             <style name="SimpleTag target=&#34;http://www.example.com/financing /uk&#34;"><style name="URL_bold">ibm.com</style></style>
            <style name="italic">
              Reference for this book<style name="superscript">1</style>
            </style>
            <style name="bulletStyle1">environmental responsibility</style>
            <style name="bulletStyle1">world’s largest florist</style>
        </style>
          </Text>
  </Value><Value AttributeID="113458" AttributeName="0180 - Text - remainder">
    <Text>
      <style name="TextHead1">The current enterprise application landscape</style>
      <style name="TextStyle1">
            <character name="linebreak"/>Introduction of the Title
            <style name="bold">
              The smallest font size
              <character name="linebreak"/>
              for which kerning should be automatically adjusted.
              <character name="linebreak"/>
              and some manual work done.
            </style>
            <style name="bold">
              Reference for this book<style name="superscript">1</style>
            </style>
        </style>
        </Text>
  </Value><Value AttributeID="13335" AttributeName="0190 - Text - remainder">
    <Text>
      <style name="TextHead1">The Bussiness application landscape</style>
      <style name="TextStyle1">
            Introduction of the Title
            <style name="bold">
              The smallest font size for which kerning should be automatically adjusted and some manual work done.
            </style>
             <style name="SimpleTag target=&#34;http://www.example.com/financing /uk&#34;"><style name="URL_bold">ibm.com</style></style>
            <style name="italic">
              Reference for this book<style name="superscript">1</style>
            </style>
            <style name="bulletStyle1">none: No leader line</style>
            <style name="bulletStyle1">dot: Dotted leader line</style>
        </style>
          </Text>
  </Value></values>