簡體   English   中英

簡單類型重復時如何使用xslt轉換來轉換Xml

[英]How to convert Xml using xslt transform when simple type is repetating

我正在嘗試使用xslt轉換將一個xml轉換為另一個xml。 當有一個復雜的節點重復時,所有的節點都經過適當的變換,一切都很好。 如果簡單類型正在重復,則將轉換相同數量的節點,但是所有節點的值都是第一個節點的值。

這是xml的一部分

<GetDataResult xmlns="http://tempuri.org/">
    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">0</string>
    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">1</string>
    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">2</string>
    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">3</string>
</GetDataResult>

這是我的Xslt代碼段的一部分

<response>
    <xsl:for-each select="ns1:GetDataResponse/ns1:GetDataResult/ns2:string" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <item>
            <xsl:value-of select="/ns1:GetDataResponse/ns1:GetDataResult/ns2:string/text()" />
        </item>
    </xsl:for-each>
</response>

我為每個循環嘗試了Xslt的幾種組合,但是最終結果如下。 所有項目都具有第一個重復節點的值。

<?xml version="1.0" encoding="utf-16"?>
<Fields>
    <response>
        <item>0</item>
        <item>0</item>
        <item>0</item>
        <item>0</item>
    </response>
</Fields>

這是轉換代碼段。

    XmlDocument xslDoc = new XmlDocument();
    xslDoc.InnerXml = XsltCode;
    System.Xml.Xsl.XslTransform xslTransform = new System.Xml.Xsl.XslTransform();
    StringWriter xmlResult = new StringWriter();
    try
    {
        //Load XSL Transform Object
        xslTransform.Load(xslDoc, new XmlUrlResolver(), null);
        //Load the xsl parameter if Any
        System.Xml.Xsl.XsltArgumentList xslArgs = new System.Xml.Xsl.XsltArgumentList();
        //Call the actual Transform method
        xslTransform.Transform(xmlDoc, null, xmlResult, new XmlUrlResolver());
    }
    catch
    { }
    string firstParse = xmlResult.ToString();

使用相對XPath在每次迭代中獲取當前ns2:string的值:

<xsl:for-each select="ns1:GetDataResponse/ns1:GetDataResult/ns2:string" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <item>
        <xsl:value-of select="." />
    </item>
</xsl:for-each>

暫無
暫無

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

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