2011-05-14 12 views
13

एक्सएमएल फ़ाइल 1 का उपयोग करने के लिए एक्सएमएल फ़ाइल परिवर्तित:एक और एक्सएमएल फ़ाइल XSLT

<?xml version="1.0"?> 
<rentalProperties> 
    <property contact ="1"> 
     <type>House </type> 
     <price>420</price> 
     <address> 
      <streetNo>1</streetNo> 
      <street>Wavell Street</street> 
      <suburb>Box Hill</suburb> 
      <state>VIC</state> 
      <zipcode>3128</zipcode> 
     </address> 
     <numberOfBedrooms>3</numberOfBedrooms> 
     <numberOfBathrooms>1</numberOfBathrooms> 
     <garage>1</garage> 
    </property> 

एक्सएमएल फ़ाइल 2:

<?xml version="1.0"?> 
<rentalProperties> 
    <property contact ="1"> 
     <type>House </type> 
     <price>420</price> 
     <address>1 wavell street,Box Hill,VIC,Australia</address> 
     <numberOfBedrooms>3</numberOfBedrooms> 
     <numberOfBathrooms>1</numberOfBathrooms> 
     <garage>1</garage>  
    </property> 

कैसे मैं एक्सएमएल fle 2 के लिए xml फ़ाइल 1 परिवर्तित xslt का उपयोग कर देना चाहिए? मैं पते को एक पंक्ति के रूप में प्रस्तुत करना चाहता हूं और लाइन के अंत में एक नई विशेषता [देश-ऑस्ट्रेलिया] जोड़ना चाहता हूं। मैंने बाकी सब किया। मैं पता पंक्ति के साथ संघर्ष कर रहा हूँ

XSLT फ़ाइल:

<address> 
    <xsl:for-each select="address/*"> 
     <xsl:value-of select="."/>, 
    </xsl:for-each> 
    Australia 
</address> 

यह xml1 में पता टैग के सभी बच्चों से अधिक लूप:

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" type="text/css" href="style.css"> 
    <xsl:template match="/"> 
     <rentalProperties> 
      <property> 
       <xsl:attribute name="contact"><xsl:value-of select='@contact'/></xsl:attribute>  
       <type><xsl:value-of select="type"/></type> 
       <price><xsl:value-of select="price"/></price> 
       <numberOfBedrooms><xsl:value-of select="numberOfBedrooms"/></numberOfBedrooms> 
       <numberOfBathrooms><xsl:value-of select="numberOfBathrooms"/></numberOfBathrooms> 
       <garage><xsl:value-of select="garage"/></garage>  
      </property>  
     </rentalProperties>  
    </xsl:template> 
</xsl:stylesheet> 

उत्तर

1

आप की तरह कुछ कोशिश कर सकते हैं।

+0

यू बहुत बहुत – shavinda

3

आप

<xsl:template match="address"> 
    <xsl:value-of select="streetNo" /> 
    <xsl:text> </xsl:text> 
    <xsl:value-of select="street" /> 
    <xsl:text>,</xsl:text> 
    <xsl:value-of select="suburb" /> 
    <xsl:text>,</xsl:text> 
    <xsl:value-of select="state" /> 
    <xsl:text>,</xsl:text> 
    <xsl:value-of select="zipcode" /> 
</xsl:template> 

का उपयोग कर पता ब्लॉक के लिए एक नया टेम्पलेट लागू करने और

<xsl:apply-templates select="address" /> 

से कॉल करने की <numberOfBedrooms> तत्व से पहले हो सकता है। यह concat फ़ंक्शन का उपयोग करके भी किया जा सकता है, जबकि सही वाक्यविन्यास मुझे अभी याद नहीं है।

+0

बहुत – shavinda

25

यह परिवर्तन:

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

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

<xsl:template match="address"> 
    <xsl:copy> 
    <xsl:value-of select= 
    "concat(streetNo, ' ', street, ',', 
      suburb,',', state,', Australia') 
    "/> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="address/node()"/> 
</xsl:stylesheet> 

जब प्रदान की XML दस्तावेज़ पर लागू:

<rentalProperties> 
    <property contact="1"> 
     <type>House </type> 
     <price>420</price> 
     <address>1 Wavell Street,Box Hill,VIC, Australia</address> 
     <numberOfBedrooms>3</numberOfBedrooms> 
     <numberOfBathrooms>1</numberOfBathrooms> 
     <garage>1</garage> 
    </property> 
</rentalProperties> 
0:

<rentalProperties> 
    <property contact ="1"> 
     <type>House </type> 
     <price>420</price> 
     <address> 
      <streetNo>1</streetNo> 
      <street>Wavell Street</street> 
      <suburb>Box Hill</suburb> 
      <state>VIC</state> 
      <zipcode>3128</zipcode> 
     </address> 
     <numberOfBedrooms>3</numberOfBedrooms> 
     <numberOfBathrooms>1</numberOfBathrooms> 
     <garage>1</garage> 
    </property> 
</rentalProperties> 

चाहता था, सही परिणाम का उत्पादन

स्पष्टीकरण: identity rule का उपयोग और ओवरराइड करना।

+0

असाधारण संदर्भ और अच्छे काम के लिए यहाँ यू धन्यवाद धन्यवाद – Eon

-1
<rentalProperties> 
    <property contact="1"> 
     <type>House </type> 
     <price>420</price> 
     <address>1 Wavell Street,Box Hill,VIC,3128</address> 
     <numberOfBedrooms>3</numberOfBedrooms> 
     <numberOfBathrooms>1</numberOfBathrooms> 
     <garage>1</garage> 
    </property> 
</rentalProperties> 
+2

कोड-केवल जवाब लगभग हमेशा कुछ व्याख्यात्मक वाक्य जोड़कर सुधार किया जा सकता; आप ऐसा करने के लिए अपने उत्तर [संपादित करें] कर सकते हैं। –

+0

यहां समाधान क्या है? –