簡體   English   中英

如何在xslt中對XML的xpath使用正則表達式?

[英]How to use regular expression for xpath of XML in xslt?

我的XML如下:

<pi:Additional_Information>
 <pi:Allocation_Cost_Center_1 > test value cost 1 </pi:Allocation_Cost_Center_1>
 <pi:Allocation_Cost_Center_2 > test value cost 2 </pi:Allocation_Cost_Center_2>
 <pi:Allocation_Cost_Center_3 > test value cost 3 </pi:Allocation_Cost_Center_2>
 <pi:Allocation_Cost_Center_4 > test value cost 4 </pi:Allocation_Cost_Center_2>
 <pi:Allocation_Region_1 >test value region 1</pi:Allocation_Region_1>
 <pi:Allocation_Region_2 >test value region 2</pi:Allocation_Region_2>
 <pi:Allocation_Region_3 >test value region 3</pi:Allocation_Region_3>
 <pi:Allocation_Region_4 >test value region 4</pi:Allocation_Region_4>
 <pi:Allocation_Location_1>Berlin</pi:Allocation_Location_1>
 <pi:Allocation_Location_2>Berlin</pi:Allocation_Location_2>
 <pi:Allocation_Location_3>Berlin</pi:Allocation_Location_3>
 <pi:Allocation_Location_4>Berlin</pi:Allocation_Location_4>
</pi:Additional_Information>

我需要在第一個循環中循環瀏覽所有以一個結尾的標簽,在下一個循環中,所有以2結尾的標簽,依此類推.....

我正在使用XSLT 2.0。 預期輸出如下:

(第一排)

測試值成本1測試值區域1柏林

(第二排)

測試值成本2測試值區域2柏林

(第三排)

測試值成本3測試值區域3柏林

(四排)

測試值成本4測試值區域4柏林

提前致謝。

這是您可以查看的另一種方式:

<xsl:template match="/pi:Additional_Information">
    <xsl:variable name="nodes" select="*" />
    <xsl:variable name="rows" select="count($nodes) div 3" />
    <xsl:for-each select="0 to $rows - 1">
        <xsl:value-of select="$nodes[(position() - 1) mod $rows = current()]"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

假設總是有3種類型的節點(因此每行3個值),並且它們按類型和行進行排序。

否則,您可以使用:

<xsl:template match="pi:Additional_Information">
    <xsl:for-each-group select="*" group-by="tokenize(name(), '_')[last()]">        
        <xsl:value-of select="current-group()"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each-group>
</xsl:template>

這將根據節點名稱中最后一個下划線之后的節點對節點進行分組。

在XSLT 2.0中:

   <xsl:template match="pi:Additional_Information">
    <xsl:for-each-group select="*" group-by="replace(name(), '[A-Za-z_]', '')">
     <loop index="{current-grouping-key()}">
      <xsl:apply-templates select="current-group()"/>
     </loop>
    </xsl:for-each-group>
  </xsl:template>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM